Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  61] [ 4]  / answers: 1 / hits: 26263  / 12 Years ago, sun, october 14, 2012, 12:00:00

By Default $.getScript() disables caching and you can use $.ajaxSetup and set caching to true. When testing if the script is actually cached with Firebug most of the time the script is coming back at 200 (Which means the script is a fresh copy) and one in maybe 20 or 30 times it will come back 304 (meaning it used a cached version). Why is it getting a new copy the vast majority of the time?



$.ajaxSetup({
cache: true
});

$.getScript( scriptFile );


The files that getScript retrieves have not been edited and the requests are a page change apart.


More From » jquery

 Answers
21

There is an error as of the date this question was posted where both Firefox and Chrome would state that a script is not being loaded from Cache when it indeed is. As of the date of this answer this issue still exists. The easiest way to test is to use console.log and send out a version number.



To cache a dynamically loaded script it it simply done by using the following code.



function onDemandScript ( url, callback ) {
callback = (typeof callback != 'undefined') ? callback : {};

$.ajax({
type: GET,
url: url,
success: callback,
dataType: script,
cache: true
});
}


For development you should comment out cache: true.


[#82560] Friday, October 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kerryoliviaa

Total Points: 221
Total Questions: 102
Total Answers: 117

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;