Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [JavaScript/AJAX] <form> + innerHTML issues...

  1. #1
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    [JavaScript/AJAX] <form> + innerHTML issues...

    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'.

    Code:
    // 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;
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2

    [JavaScript/AJAX] <form> + innerHTML issues...

    Which browser are you using? I think IE 5 and 6 have a bug related to innerHTML.

  3. #3
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    [JavaScript/AJAX] <form> + innerHTML issues...

    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. :?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #4

    [JavaScript/AJAX] <form> + innerHTML issues...

    Maybe MooTools can do the DOM switch for you. Otherwise I just don't know. http://mootools.net/

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    [JavaScript/AJAX] <form> + innerHTML issues...

    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?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    [JavaScript/AJAX] <form> + innerHTML issues...

    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
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    [JavaScript/AJAX] <form> + innerHTML issues...

    Well that is a solution. But will I have any problems with JavaScript inside and/or outside of the created iframe?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8

    [JavaScript/AJAX] <form> + innerHTML issues...

    And you totally can't read what MooTools is doing right...

  9. #9
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    [JavaScript/AJAX] <form> + innerHTML issues...

    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.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  10. #10
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    [JavaScript/AJAX] <form> + innerHTML issues...

    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/ho...-into-dom.html

    Or to save you some time page loading...
    Code:
    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.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •