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>