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: