SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2004
    Location
    London
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xmlhttp requesting javascript

    Hi,

    I'm using xmlhtp request to populate a div with some external content. I'm fine with getting it to display html and have that refresh only a portion of a page.

    The real trouble I'm having is this - the external content has some javascript that I'd like to execute, so, does anyone know how I can dynamically execute javascript as the xmlhttp section of the page refreshes itself?

    I've tried to create a dummy function and then populate that with the javascript, but that doesn't seem to work. I guess what I'm asking for is helping to dynamically execute dynamically created javascript

  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)
    i'm not sure if i understand you correctly, but couldn't you just write the code into youre dynamically populated area, with some neat <script> tags sorrounding them ?
    eg :
    Code:
    var szURL = "...";
    var request = new XMLHttpRequest();
    request.open("GET", szURL, true);
    request.send(null);
    
    var oDiv = document.getElementById("myDiv");
    oDiv.innerHTML = "<scr" + "ipt type='text/javascript'>" + request.responseText + "</script>";

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've run into problems with the above approach as far as getting dynamically loaded script to run. Might try using an actual http request for a new script, as described here. Always works.
    ::: certified wild guess :::

  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)
    obviously it's ie only, but apart from that, what problems may result from using that method ?

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You talkin' to me? I'm positive I've tried writing top-level script statements (outside of functions) to innerHTML like that, and the statements were not executed. Presumably something to do with the manner in which the HTML is parsed. Could be mistaken; have you been successful with it?

    The 'dynamic script' approach just seems semantically attractive, as you're specifically downloading JS anyway. Don't think it can can be classified as a 'hack', at least in the pejorative sense. Those are neat tags, though.
    ::: certified wild guess :::

  6. #6
    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)
    Quote Originally Posted by adios
    I'm positive I've tried writing top-level script statements (outside of functions) to innerHTML like that, and the statements were not executed. Presumably something to do with the manner in which the HTML is parsed. Could be mistaken; have you been successful with it?
    hum ... i don't recall ... but then an eval() could cut it.

    I totally agree that your approach is a much better one, but the question was specifically directed against xmlhttp, so i suppose there is some reason for that.

    you might want to have a look at this :
    http://www.sitepoint.com/blog-post-view.php?id=191776

  7. #7
    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)
    one probelm btw with the method you described is that the script is loaded asynchronously. this could cause trouble if the suceeding code relies on the code included.

  8. #8
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dynamically execute dynamically created js

  9. #9
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops! pl ignore my prev mail !

    Q : dynamically execute dynamically created javascript
    A : scenario 1. fresh bunch of js file - just call the js file from server. eg; by creating dynamically ' <SCRIPT language ="javascript" src="..."> ' node and appendChild() it to the document. Alternatively write the whole js code into the <SCRIPT> node & appendChild(). THough this is complex & prone to errors.

    scenario 2: eval() function can evaluate & execute a js which is in text format.

    cheers
    kkr_nair


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
  •