Home > Web Front-end > JS Tutorial > body text

Detailed explanation of jquery getScript dynamic loading JS method improvement_jquery

WBOY
Release: 2016-05-16 17:48:14
Original
983 people have browsed it
Copy code The code is as follows:

$.getScript(url,callback)

This method is a method provided by jquery itself for dynamically loading js. When a website needs to load a large amount of js, dynamically loading js is a better method. When a certain function is needed, the corresponding js is loaded.
However, I found some dissatisfaction during use.


Every time you need to execute this function, you will request this js. Isn’t this a disservice?
So I found the API description of Jquery official website 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 into 304, thus using the client's cache:
Copy code The code is as follows:

$.ajaxSetup ({
cache: true
});

So, you will find that every time you call this function, it becomes as follows:


Every time js is called, the following parameters like "?_=13126578" are gone, and the status is Not Modified.
But I am a bit of a germophobe. Every time I use this function, although the server no longer needs to return the entire js file, it still has to request the server every time, which always makes me feel uncomfortable. Hence the title of this blog was born.
Without further ado, let’s start with the code:
Copy the code The code is as follows:

< ;!DOCTYPE html >












The code in t1.js is also a function
Copy the code The code is as follows:

function alertMe() {
alert('clicked me');
}

At this point, the entire transformation is completed. When you use this function, It will only send a js request to the server once during initialization, and after the loading is completed, it will not request the server again, even if it is a 304 status code.

I am a newbie in js, please give me a pat, O(∩_∩)O~
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!