SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there any trick to make JS loaded with Ajax work?

    Hello,

    I'm using Ajax a lot on my website and I'd like to load JS/HTML with Ajax, not only HTML. The problem is when I load JS with Ajax, it doesn't work.

    I wonder, is there any way to make it work?

  2. #2
    SitePoint Zealot Shpook's Avatar
    Join Date
    May 2007
    Location
    Hollywood, FL, USA
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Generally, any JavaScript that's not a function should be executed when it's downloaded. There are many methods to use JS that's loaded asynchronously, it depends on if it needs to run immediately, access the DOM, or be added to an existing object. A good explanation with examples is http://ajaxpatterns.org/On-Demand_Javascript. Read that, and hopefully it will help.
    Life would be so much easier if we
    could just look at the source code.
    -Dave Olson

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using mootools framework you can load external assets as needed
    Code:
    new Asset.javascript('/scripts/myScript.js', {id: 'myScript'});

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You don't need Ajax to download and run JavaScript. Simply calling the appropriate DOM commands to add a script tag to the page will download and run the file referenced by the src attribute.
    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="^$">

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    You don't need Ajax to download and run JavaScript. Simply calling the appropriate DOM commands to add a script tag to the page will download and run the file referenced by the src attribute.
    How can you make sure that when you use code from that js file it is loaded? Besides using the ie-only onreadystatechange?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    See http://javascript.about.com/library/bladdjs.htm where you will find a JavaScript function that will download and run another JavaScript file whenever it is run without using any Ajax calls at all. The file will load and then run anything within the file that is not inside of a function.
    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 Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    addJavascript('myjs.js'); //has a variable valled myVar
    alert(myVar); //gives my an undefined error because myjs.js isn't loaded
    setTimeout(function(){alert(myVar);}, 500); // works because myjs.js loaded

    See what I mean now? Note that I didn't test the above code.


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
  •