SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member markdr's Avatar
    Join Date
    Jul 2006
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Executing javascript returned from AJAX call

    I am developing a personalised homepage system that loads in modules using AJAX.

    Basically innerHTML is used to insert the returned AJAX data into a DIV for a module.

    However if the module contents contains javascript, it is not executed - nothing happens.

    So, in this basic example -
    HTML Code:
    <script language="javascript" type="text/javascript">
    alert("test");
    </script>
    - why is alert("test") is not called? Is there anything I can do to cause this javascript content to be executed when the AJAX call is complete?

    Thanks in advance.
    "When hell freezes over, it'll be a cool place to snowboard!"

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The response of an XMLHttpRequest is just plain text.
    It's up to you to process it.
    Mostly you use XMLHttpRequest to get a response in an XML-like structure, so you can use that information dynamically.
    Do not confuse XMLHttpRequest with a replacement of the source of a simple (i)frame. The contents of a frame or iframe is directly parsed by the browser, the response of an XMLHttpRequest will be stored in a JavaScript variable for you to use.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Member markdr's Avatar
    Join Date
    Jul 2006
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply!

    I actually considered using IFRAMEs instead in the early days of this project, but for several reasons I went for AJAX (sortable javascript library performed badly with IFRAMEs, height of modules needed to change according to contents, much easier with DIVs than IFRAMES)

    I was thinking of using eval() to parse javascript, but at the moment it is inline with HTML code so I'm not sure whether this would work or not.

    Anyone got any other thoughts that might help?
    "When hell freezes over, it'll be a cool place to snowboard!"

  4. #4
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just reading the prototype.js documentation I found this
    Scroll down to Evaluating a JavaScript response
    Another interesting part further down the page: Updating your page dynamically with Ajax.Updater
    FOR SALE: 1 set of morals, never used, will sell cheap

  5. #5
    SitePoint Member markdr's Avatar
    Join Date
    Jul 2006
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hexburner View Post
    Just reading the prototype.js documentation I found this
    Scroll down to Evaluating a JavaScript response
    Another interesting part further down the page: Updating your page dynamically with Ajax.Updater
    Thanks for leading me to that page. I am already using prototype coincidentally, as part of script.aculo.us, but for some reason using a different AJAX library. I think i'm going to switch to prototype for my AJAX calls having read that page...some very useful features.

    As I read it, the Ajax.Updater looks to be exactly what I'm looking for, with the evalScripts option on - it will parse scripts contained in the HTML returned.

    Thanks a lot.
    "When hell freezes over, it'll be a cool place to snowboard!"


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
  •