I am trying to imported some javascript files from some code hosts.
$.when(
$.getScript('http://pingzi.googlecode.com/svn-history/r30/branches/wangqi/web/jquery.window.min.js'),
$.getScript('http://mottie.github.com/tablesorter/js/jquery.tablesorter.js'),
$.getScript('http://tconnell.com/samples/scroller/lib/jquery.tablesorter.scroller.js'),
$.getScript('http://code.highcharts.com/stock/highstock.js'),
$.Deferred(
function(deferred) {
$(deferred.resolve);
}
)
).done(function() {
// my function goes here....
});
When I try to call these URLs to import js files, the URLs will append ?_=1344036242417
and then I could not actually get to the script file I want.
i.e.
"NetworkError: 404 Not Found - http://pingzi.googlecode.com/svn-history/r30/branches/wangqi/web/jquery.window.min.js?_=1344036242417"
Any one have ideas how to bypass this problem? Thank you in advance.
That's because caching in ajax is turned off by default in jQuery, to turn it on and remove the querystring do :
$.ajaxSetup({
cache: true
});
but that could also affect other ajax calls that you don't want to cache, there's a lot more on this in the docs for getScript, and there's even a little how-to on creating a cached getScript function called cachedScript.
You can also enable caching in $.getScript by redefining the function with a new option to turn the cache on/off by passing true or false :
$.getScript = function(url, callback, cache){
$.ajax({
type: "GET",
url: url,
success: callback,
dataType: "script",
cache: cache
});
};
jQuery
have an automatic caching mechanism for such queries.
If you do not want the extra param to be added use following setup:
$.ajaxSetup({
cache: true
});
Source: http://api.jquery.com/jQuery.getScript/#caching-requests
jQuery is automatically adding the _=1344036242417
, which breaks the URL. Note:
To prevent jQuery from adding that parameter: Ajax get request with useless parameter. To summarize that answer, use $.ajaxSetup
before calling $.getScript()
to set cache: true
.
Be [sic] default,
$.getScript()
sets the cache setting tofalse
. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested. You can override this feature by setting the cache property globally using$.ajaxSetup()
:$.ajaxSetup({ cache: true });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With