JquerygetScript dynamic loading JS method improvement details _ jquery-js tutorial

Source: Internet
Author: User
Many friends need to use the getScript method to dynamically load JS. This article will introduce the implementation method of this function in detail. The Code is as follows:


$. GetScript (url, callback)


This method is provided by jquery itself for dynamic loading of js. When a website needs to load a large number of js files, dynamic loading of js is a good method. When a function is required, the corresponding js files are loaded.
However, I found some unsatisfactory points in the use process.


Every time you need to execute this function, you will request this js once. Isn't that helpful?
So find the Jquery official site of the API description http://api.jquery.com/jQuery.getScript/
In fact, this method is an encapsulation of the ajax method. You can use the cache of the ajax method to change the http status 200 to 304, so as to use the Client Cache:

The Code is as follows:


$. AjaxSetup ({
Cache: true
});


Therefore, we will find that every time we call this function, it becomes as follows:


This is similar "? The _ = 13126578 "parameter is no longer available and the status is Not Modified.
But I am a little "clean". Every time I use this function, although the server does not need to return the entire js file any more, I still need to request a server every time, and I always feel uncomfortable. The title of this blog was born.
Not to mention, first go to the Code:

The Code is as follows:







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.