Asynchronously loading JavaScript script solutions with jquery _jquery

Source: Internet
Author: User
The JavaScript loader is a powerful and useful tool in WEB development. Several of the current popular loaders, such as Curljs, LABJS and Requirejs are widely used. They are powerful, but in some cases they can have simpler schemes.

If you are using JQuery, there is a built-in method that can be used to load the script. You can use this method if you want to delay loading plug-ins or any other type of script. Here's how to use it.

Implementation Methods

JQuery has a built-in Getscript method to load a script, and there are several ways to handle the returned results. The most basic usage jquery.getscript looks like this:
Copy Code code as follows:

Jquery.getscript ("/path/to/myscript.js", function (data, status, JQXHR) {

/*
After the script has been loaded and executed, you can do some processing
*/

});

The Getscript method returns a Jqxhr object, so you can use this:
Copy Code code as follows:

Jquery.getscript ("/path/to/myscript.js")
. Done (function () {
/* After successful processing * *
})
. Fail (function () {
* * After failure to perform the processing/
});

The most common scenario for using Jquery.getscript is to delay loading a plug-in and call it after loading:
Copy Code code as follows:

Jquery.getscript ("Jquery.cookie.js")
. Done (function () {
Jquery.cookie ("Cookie_name", "value", {expires:7});
});

If you need to do something more advanced, such as loading multiple scripts and different types of files (text files, images, CSS files, etc.), I recommend that you switch to a more powerful JavaScript loader. If you only want to delay loading plug-ins, instead of simply loading each page, the getscript is perfect!

Caching Problems

It should be noted that when using Jquery.getscript, the script URL is automatically added to the timestamp, so that the script does not cache. So you need to set up all the requests to cache the script:
Copy Code code as follows:

Jquery.ajaxsetup ({
Cache:true
});

If you do not want to overwrite all caches with your AJAX request, it is best to use the Jquery.ajax method and set the DataType to script, for example:
Copy Code code as follows:

Jquery.ajax ({
URL: "Jquery.cookie.js",
DataType: "Script",
Cache:true
}). Done (function () {
Jquery.cookie ("Cookie_name", "value", {expires:7});
});

You need to pay special attention to caching problems when loading scripts!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.