0

I get some scripts asynchronously on my login page:

 $.when(
 $.getScript("/Scripts/View/scroll-sneak.js"),
 $.getScript("/Scripts/kendo/kendo.custom.min.js"),
 $.Deferred(function (deferred) {
 $(deferred.resolve);
 })
 ).done(function (res1, res2) {
 if (res1[1] == "success") {
 }
 if (res2[1] == "success") {
 }
 alert('all script loaded...');
 });

I have two queries here:

  1. How can I leverage browser cache here, as getScript always take fresh script.
  2. How can I have promise that this script will be available to all pages on same domain.

Alternate solutions are welcome.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Oct 10, 2014 at 7:46
0

1 Answer 1

1

Answer to your first question is set cache true. Jquery documentation page also mentions a way

jQuery.cachedScript = function( url, options ) {
 // Allow user to set any option except for dataType, cache, and url
 options = $.extend( options || {}, {
 dataType: "script",
 cache: true,
 url: url
 });
 // Use $.ajax() since it is more flexible than $.getScript
 // Return the jqXHR object so we can chain callbacks
 return jQuery.ajax( options );
};
// Usage
$.cachedScript( "ajax/test.js" ).done(function( script, textStatus ) {
 console.log( textStatus );
});

For your second question: Please clarify more, what you want to achive?

answered Oct 10, 2014 at 7:51
Sign up to request clarification or add additional context in comments.

2 Comments

what if script has size which can't be cached, then how i maintain it
If it's not cached then (Highly unlikely until nocache header is set) then it will be requested again. You need not to go that hard on leverage browser caching in this case.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.