1

If you're using jQuery, is there any reason to use one of these methods to load a JavaScript file rather than the other?

$.getScript('https://apis.google.com/js/platform.js');

vs

(function() {
  var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  po.src = 'https://apis.google.com/js/platform.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
1
  • There is no functional difference between those two examples. But getScript is obviously shorter and provides an optional callback too. async = true is the default option and is redundant in the code. Commented May 25, 2014 at 19:54

1 Answer 1

1

Yes. $.getScript performs a "script" $.ajax request and runs through the $.ajax pipeline, so if you register any settings and handlers through $.ajaxSetup or $.ajaxError they will run.

For example, the following will cause scripts from $.getScript to be cached to the local memory:

$.ajaxSetup({
  cache: true
});
Sign up to request clarification or add additional context in comments.

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.