jquery Ajax.getscript Caching problem

Source: Internet
Author: User
Tags bind getscript script tag

Add a cache switch for $.getscript ()

The code is as follows

Add cache control to Getscript method
(function ($) {
$.getscript = function (URL, callback, cache) {
$.ajax ({type: ' Get ', Url:url, Success:callback, DataType: ' Script ', Ifmodified:true, Cache:cache});
};
}) (JQuery);


$.getscript () to remove the time to automatically add timestamp

Jquery.getscript is mainly used to implement JSONP, the browser to obtain data across the domain, using the following methods

The code is as follows

$.getscript (Url,function () {...});

It hides a deep pit: automatically adds a timestamp to your URL, such as you request http://www.3lian.com, the actual access URL is "/?_=1379920176108"

If you have a special resolution to the URL, which is tragic, will waste a lot of time debug, to reject this behavior, you can use Jquery.ajax, using the following

The code is as follows

1.$.ajax ({
2. URL: ' http://m.lutaf.com ',
3. DataType: "Script",
4. Cache:true,
5. Success:function () {}
6.}); using Jquery.ajax and set cache = Ture, can remove the timestamp property alltogether.


Improvement of dynamic Loading JS "method Getscript"

The code is as follows

<! DOCTYPE HTML >
<meta charset= "Utf-8" >
<title></title>
<script src= "Jquery-1.7.2.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
A tag array that defines a global script that is used to mark whether a script has been downloaded to the local
var scriptsarray = new Array ();

$.cachedscript = function (URL, options) {
Loop Script Tag Array
For (var s in Scriptsarray) {
Console.log (Scriptsarray[s]);
If an array has been downloaded to the local
if (Scriptsarray[s]==url) {
return {//Returns an object literal, where the done is called done to correspond to the done in the following $.ajax
Done:function (method) {
if (typeof method = = ' function ') {//If the incoming argument is one of the methods
Method ();
}
}
};
}
}
This is the way jquery officially provides a getscript implementation, which means that getscript is actually an extension of the AJAX approach
Options = $.extend (Options | | {}, {
DataType: "Script",
Url:url,
Cache:true//In fact, now this cache plus and does not add much difference
});
Scriptsarray.push (URL); Put the URL address into the script tag array
return $.ajax (options);
};
$ (function () {
$ (' #btn '). Bind (' click ', function () {
$.cachedscript (' t1.js '). Done (function () {
AlertMe ();
});
});


$ (' #btn2 '). Bind (' click ', function () {
$.getscript (' t1.js '). Done (function () {
AlertMe ();
});
});
});
</script>
<body>

<button id= "BTN" > Custom caching Method </button>
<br/>
<button id= "BTN2" >getscript</button
</body>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.