views:

230

answers:

3

I'd like to know if I can load an external JS dynamically based on some condition, for example:

$(window).load(function () {
   if($.browser.msie && $.browser.version=="6.0") {
     // load ie.js
     // do stuff using ie.js
   }
});
+2  A: 

JQuery's GetScript should do it.

$.getScript("yourscirpt.js", function() {
    alert('Load Complete');
});
Michael La Voie
So if the above function is in /dir/one.js, yourscript.js should be in the same dir?
Nimbuz
The first argument is a URL, not a path. `$.getScript()` is really making an AJAX call.
kiamlaluno
A: 

Use $.getScript(url,callback); it loads the script, and executes it.

kiamlaluno
+2  A: 

If not using jquery, use this to include js

document.writeln('<script type="text/javascript" src="your.js"></script>');
Nrj