SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Pulling in HTML

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

    Pulling in HTML

    i'm using AJAX to pull in HTML from an external HTML document (cause i don't wanna use iframes). thing is, the javascript within the <script> tags doesn't execute when pulled in.

    i would like for the javascript to execute when the HTML is brought into the page.

    even a simple...

    <script type="text/javascript">
    alert("test");
    </script>

    ...doesn't work. and i've tried window.onload = function() {}, too.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That won't work. Why don't you use iframes?

  3. #3
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually - it can be done... using AJAX i can do a responseXML and grab the <script> tag and run an eval() on it. i'm pulling the responseText but i'm having problems right now getting the <script> tag using req.responseXML.getElementsByTagName("script")[0].firstChild.data;

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    responseXML is only available, if the response is content-type xml

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But if you override the mime type you can use it.

    http://developer.mozilla.org/en/docs...MimeType.28.29

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why are you using Ajax or iframes for loading additional JavaScript when you can just add the script tag to the page and pull the script in automatically. Pulling in JavaScript by adding a script tag to the page is one of the four ways of updating a web page from the server without reloading the page (ajax, iframes, and images that run server side code being the other three)
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Why are you using Ajax or iframes for loading additional JavaScript when you can just add the script tag to the page and pull the script in automatically. Pulling in JavaScript by adding a script tag to the page is one of the four ways of updating a web page from the server without reloading the page (ajax, iframes, and images that run server side code being the other three)
    i'm using ajax because i have a <div> that i'm populating with various html files on the fly - yet each html file needs it's own javascript to run when it's loaded. i don't wanna use iframes cause i think they're a pain to mess with it and loading into a div i can make it conform to the page much easier.

    i've had zero problems with bringing in the html files or styling...just having the html document's javascript execute.

  8. #8
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    But if you override the mime type you can use it.

    http://developer.mozilla.org/en/docs...MimeType.28.29
    that helped me out a ton, man...thanks! here's what i did, basically...

    in the ajax function
    Code:
    pagereq.onreadystatechange = loadpage;
            pagereq.open("GET", url, false);
            pagereq.overrideMimeType("text/xml");
            pagereq.send("");
    within my loadpage function
    Code:
    if (pagereq.responseXML.getElementsByTagName("script").length) {
                    alert(pagereq.responseXML.getElementsByTagName("script").item(0).innerHTML);
                }
    although i'm obviously not going to use alert() for the final - i just wanted to make sure that the <script> text was being passed along.


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
  •