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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: