Jquery ajax. getscript cache Solution

Source: Internet
Author: User
Tags getscript

In jquery's ajax, getscript is a function that enables cross-origin loading to obtain data. Next I will introduce some usage and possible solutions to getscript.

Add a cache switch for $. getScript ()

 

The Code is as follows: Copy code
// 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 );


For $. getScript () to remove the automatically added time timestamp

Jquery. getScript is mainly used to implement jsonp. the browser obtains data across regions. The usage is as follows:

The Code is as follows: Copy code

$. GetScript (url, function (){...});

It hides a deep trap: automatically add timestamp to your url, for example, you request http://www.bKjia. c0m, the actual access url is "/? = 1379920176108"

If you have a special resolution on the url, it will be a tragedy. It will waste a lot of time debugging. to reject this behavior, you can switch to jquery. ajax. The usage is as follows:

The Code is as follows: Copy code

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, you can remove the timestamp property alltogether.


Improvement on dynamic loading of JS [method getScript]

The Code is as follows: Copy code

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> </title>
<Script src = "jquery-1.7.2.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
// Defines the mark array of a global script to mark whether a script has been downloaded to the local device.
Var scriptsArray = new Array ();

$. CachedScript = function (url, options ){
// Mark the array with cyclic scripts
For (var s in scriptsArray ){
// Console. log (scriptsArray [s]);
// If an array has been downloaded to the local device
If (scriptsArray [s] = url ){
Return {// returns an object literal. The done is called done to correspond to the done in $. ajax.
Done: function (method ){
If (typeof method = 'function') {// if the input parameter is a method
Method ();
}
}
};
}
}
// Jquery officially provides methods similar to getScript implementation, that is, getScript is actually an extension of ajax methods.
Options = $. extend (options | | {},{
DataType: "script ",
Url: url,
Cache: true // In fact, this cache addition is not much different from not adding
});
ScriptsArray. push (url); // put the url address in 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>
</Head>
<Body>

<Button id = "btn"> Custom cache method </button>
<Br/>
<Button id = "btn2"> getScript </button>
</Body>
</Html>

Related Article

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.