1

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
  • The use of $.browser.msie like that is deprecated in jQuery 1.3+. Commented Dec 22, 2009 at 4:19
  • 2
    @cletus: no it isn't. jQuery.browser, although no longer used by jQuery internally, is available to user scripts for the indefinite future: docs.jquery.com/Utilities/jQuery.browser Commented Dec 22, 2009 at 4:25

3 Answers 3

4

JQuery's GetScript should do it.

$.getScript("yourscirpt.js", function() {
    alert('Load Complete');
});
Sign up to request clarification or add additional context in comments.

2 Comments

So if the above function is in /dir/one.js, yourscript.js should be in the same dir?
The first argument is a URL, not a path. $.getScript() is really making an AJAX call.
3

If not using jquery, use this to include js

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

1 Comment

could you also use this notation? <script type="text/javascript" src="your.js"/> so that you close the script tag and dont have to leave the value empty
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.