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

<meta charset= "Utf-8" >
<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",
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 ();

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

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.