SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    531
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    responseXML and getElementsByTagName in IE

    ok - basically i'm pulling HTML into a page via AJAX. in these HTML pages there could be some javascript and if there is javascript i want to execute the script.

    the following works perfect in firefox. in IE the page will be created perfectly, but it's not finding any <script> tags.

    Code:
    // these are parts pulled out of the overall script
    
        if (pagereq) {
            pagereq.onreadystatechange = loadpage;
            pagereq.open("GET", url, false);
            if (msie < 0) { pagereq.overrideMimeType("text/xml"); } // override the mime type so we can find any <script> tags - only firefox needs this
            pagereq.send("");
        }
    
    function loadpage() {
        if (pagereq.readyState == 1) { //loading
            /* start any loading actions */
        } else if (pagereq.readyState == 4) {
            if (pagereq.status == 200) {
                document.getElementById(content).innerHTML = pagereq.responseText;
                
                if (pagereq.responseXML.getElementsByTagName("script").length) { // see if the page has any javascript
                    eval(pagereq.responseXML.getElementsByTagName("script")[0].innerHTML); // execute the javascript
                }
            }
        }
    }
    pagereq.responseXML.getElementsByTagName("script").length returns 1 for firefox and 0 for IE in an HTML document containing <script>.

  2. #2
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    531
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone know why IE isn't able to find the <script> tag within the responseXML?

  3. #3
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    531
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2007
    Location
    New London, CT
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try putting the script inside of the CDATA tag. I'm not too familiar with the XHR thing yet but i think that may be part of the problem

  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found your post while searching for a solution to the same problem. I just fixed it by changed the Content-Type in the header of the XML document I was working with from application/xhtml+xml to text/xml. I'm able to use getElementsByTagName on it now, so hopefully the same solution works for you without breaking something else you're doing.

    Good luck,
    Brian


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
  •