PDA

View Full Version : [JavaScript/AJAX] <form> + innerHTML issues...



WILL
15-08-2007, 09:38 PM
Hey folks!

I figured I'd post it here first, just to see. ;)

Ypu, I've been playing around with some AJAX stuff lately. XMLHttpRequest is quite the nifty little object.

I've managed to make a page that will load other pages within a div tag without having to reload the page. "Wooow, neato!" :lol:

Here is the royal pain in the butt now. By using the .InnerHTML property to assign the new contents for the div tag, it won't allow any of my form tags or elements to pass through it.

Any ideas?

Here is my little function that will take a call like "ajaxpage(url)" and load the assigned url to a div block called 'page'.


// XMLHttpRequest Object Function
function ajaxpage&#40;url&#41;
&#123;
var http_request = false;

if &#40;window.XMLHttpRequest&#41; &#123; // Mozilla, Safari,...
http_request = new XMLHttpRequest&#40;&#41;;
if &#40;http_request.overrideMimeType&#41; &#123;
http_request.overrideMimeType&#40;'text/xml'&#41;;
&#125;
&#125; else if &#40;window.ActiveXObject&#41; &#123; // IE
try &#123;
http_request = new ActiveXObject&#40;"Msxml2.XMLHTTP"&#41;;
&#125; catch &#40;e&#41; &#123;
try &#123;
http_request = new ActiveXObject&#40;"Microsoft.XMLHTTP"&#41;;
&#125; catch &#40;e&#41; &#123;&#125;
&#125;
&#125;

if &#40;!http_request&#41; &#123;
alert&#40;'Unfortunatelly you browser doesn\'t support this feature.'&#41;;
return false;
&#125;
http_request.onreadystatechange = function&#40;&#41; &#123;
if &#40;http_request.readyState == 4&#41; &#123;
if &#40;http_request.status == 200 || window.location.href.indexOf&#40;"http"&#41; == -1&#41; &#123;
LoadPage&#40;http_request, 'page'&#41;;
&#125; else &#123;
alert&#40;'There was a problem with the request.&#40;Code&#58; ' + http_request.status + '&#41;'&#41;;
&#125;
&#125;
&#125;
http_request.open&#40;'GET', url, true&#41;;
http_request.send&#40;null&#41;;
&#125;

// Replace Element Contents
function LoadPage&#40;page_request, element_id&#41;
&#123;
document.getElementById&#40;element_id&#41;.innerHTML = page_request.responseText;
&#125;

cronodragon
15-08-2007, 11:50 PM
Which browser are you using? I think IE 5 and 6 have a bug related to innerHTML.

WILL
16-08-2007, 02:06 AM
I use FireFox, but from what I've been reading so far this is common for all browsers.

Thing is I need a solution for all (--well current IE, FF, Opera & Safari) browsers not just one or the other. :?

Robert Kosek
16-08-2007, 02:22 AM
Maybe MooTools can do the DOM switch for you. Otherwise I just don't know. http://mootools.net/

WILL
16-08-2007, 04:22 AM
Well as helpful as a framework or library like that might do the hard stuff for me, I won't learn anything from it. At least not what I want to know here.

It's been suggested by some articles read in my research that document.getElementById('element_id').InnerHTML is not the DOM friendly way of altering the guts of an element block. This is sort of proof as to why. But I'm madly curious if there is not a much better way than to go all out and make my own HTML parser just to create each element and text node that makes up the entire page.

Or is this exactly what I need to do? :P

savage
16-08-2007, 06:58 AM
Why not use an iFrame within the Div and pass the form page in that way.

There seem to have some easy to follow examples here -
http://www.cryer.co.uk/resources/javascript/script4.htm

WILL
16-08-2007, 03:25 PM
Well that is a solution. But will I have any problems with JavaScript inside and/or outside of the created iframe?

Robert Kosek
16-08-2007, 03:57 PM
And you totally can't read what MooTools is doing right... :P

WILL
16-08-2007, 04:16 PM
I just found out that thats not a very good solution if I want to maintain contectivity between the guts of the new page inside the div and the javascript to update the page inside... :?

As far as MooTools goes, I'm not finding any functions inside of there that does what I'm asking about. :read: :scratch:

It seems like my best option is to make my own HTML parser in JavaScript. Which in a way is kind of funny considering that this is under a Browser. You'd think that I'd already have some kind of function that would create (or help create) the HTML for me. :P

WILL
17-08-2007, 07:42 PM
Hey guys, just found this little tid-bit out there. It was very helpful as a way around the more odd behavior of dynamically loading form tags into a div without the use of .innerHTML.

http://jszen.blogspot.com/2007/02/how-to-parse-html-strings-into-dom.html

Or to save you some time page loading...

var range = document.createRange&#40;&#41;;
range.selectNode&#40;document.body&#41;;
var parsedHTML = range.createContextualFragment&#40;SomeStringOfHTML&#41;;

document.body.appendChild&#40;parsedHTML&#41;

Thanks for the help so far guys. :thumbup:

I do have a side question though... does anyone know if this code is workable for IE/Safari/Opera as well as FF? I've only managed to test it and see it working on FireFox 2.0.

savage
17-08-2007, 08:32 PM
post it somewhere and I can test it with IE6 & 7.

WILL
18-08-2007, 02:54 AM
Ok, I'll post up a quick test tomorrow-ish for you to try. :)