Original link: Loading Scripts with JQuery
The JavaScript loaders Loader is simple, powerful, and very useful. I've introduced some of them on my blog, such as Curljs and LABJS, and also used Requirejs and Dojo loaders in personal projects. They are so powerful, but in some cases it may be overkill. If you're using jquery, you'll find that jquery has built-in methods for loading a single script, which might come in handy if you want to defer loading plugins or other types of scripts. Here's how to use it!
The JQuery JavaScript
jquery provides a getScript method for loading scripts, and results can be processed in several ways. The basic usage of Jquery.getscript is as follows:
Jquery.getscript ("/path/to/myscript.js", function (data, status, JQXHR) {/ * When the script is loaded and executed, you can perform some special processing here. */ });
The callback function of the Getscript method provides a jqxhr parameter. Of course, it can be handled as follows:
Jquery.getscript ("/path/to/myscript.js") . Done (function () { /* * Load succeeded /}) . Fail (function () { / * Load failed, do fall back processing */ });
The most common usage scenario for jquery.getscript is to delay loading a plug-in and use it after the load is complete:
Jquery.getscript ("Jquery.cookie.js") . Done (function () { Jquery.cookie ("Cookie_name", "value", {expires:7 }); });
If you need more advanced loading capabilities, such as loading multiple scripts at the same time, or loading different types of files (text, images, CSS, etc.), you should switch to a JavaScript loader.
Sometimes we want to delay loading a plugin after a certain condition is met, so $.getscript is a great solution.
Original link: http://blog.csdn.net/renfufei/article/details/18039237
Loading script scripts with jquery