SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Jan 28, 2008, 18:52 #1
- 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?
-
Jan 28, 2008, 19:31 #2
- 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
-
Jan 31, 2008, 18:56 #3
- 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'});
-
Jan 31, 2008, 19:01 #4
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- 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="^$">
-
Feb 1, 2008, 00:43 #5
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 1, 2008, 02:43 #6
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- 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="^$">
-
Feb 1, 2008, 06:03 #7
- 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