Translated by: Tie
Translated on: February 1, January 9, 2014
Original Article Date: January 1, January 6, 2014
Original article:Loading Scripts with jQuery
The JavaScript loaders loader is simple, powerful, and very useful. I have introduced some of them in my blog, such as curljs and LABjs. I also use RequireJS and Dojo to load programs in my project. They are so powerful, but in some cases it may be a cow. If you are using jQuery, you will find that jQuery has built-in methods to load a single script. If you want to delay loading plug-ins or other types of scripts, This method may be used. The following describes how to use it!
The jQuery JavaScript
JQuery provides the getScript method to load scripts. You can perform result processing in multiple ways. The basic usage of jQuery. getScript is as follows:
JQuery. getScript ("/path/to/myscript. js ", function (data, status, jqxhr) {/* after the script is loaded and executed, You can execute some special processing here. */});
The callback function of the getScript method provides a jqxhr parameter. Of course, it can be processed as follows:
JQuery. getScript ("/path/to/myscript. js "). done (function () {/* loaded successfully */}). fail (function () {/* loading failed, fall back processing */});
The most common use case of jQuery. getScript is to delay loading a plug-in and use it after loading:
jQuery.getScript("jquery.cookie.js") .done(function() { jQuery.cookie("cookie_name", "value", { expires: 7 });});
If you need more advanced loading functions, such as loading multiple scripts at the same time or loading different types of files (text, images, css, etc.), you should change to a JavaScript loader.
Sometimes $. getScript is a good solution to delay loading a plug-in after certain conditions are met.