Dynamically Load JavaScript With jQuery

Another wordpress blogger, Stephen at colourgrey.wordpress.com has an excellent article on dynamically loading javascript with jQuery.

The key here, is the ajaxSetup function.

$.ajaxSetup({async: false});
$.getScript(MVCRoot+fileName+'.js');
$.ajaxSetup({async: true});

Turning off asynchronous ajax allows the scripts to load completely before the rest of the page tries to access the contained code.

There was a great video on code.google.com as well that showed the speed advantage of using this method as well.

Leave a Reply