Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am using $.getScript to load some of my program code. My code looks something like this:

var mainJs = "/main.js";
$.getScript( mainJs )
    .then(function () {
        console.log("main.js loaded");
    });

The code works fine. However, I am thinking to use Angular-cache to do caching control on it. Is it possible to be done?

share|improve this question
    
Are you trying to prevent it being loaded again or trying to save another trip to server? – charlietfl Aug 26 '15 at 15:42
    
@charlietfl I want to prevent it from loading again, even by next time user is visiting. It only try to get from server again when the cache is expired by angular-cache. – user1995781 Aug 26 '15 at 15:46
    
angular cache doesn't store anything for next visit either. i think you have your concepts of caching mixed up a bit. Caching this won;t prevent the script from running again within the same app session....is that your goal? If so you will need some service to keep track if it has been run or not – charlietfl Aug 26 '15 at 15:53
    
@charlietfl Isn't that angular cache can store as localStorage? Correct me if I am wrong.. – user1995781 Aug 26 '15 at 15:58
    
no ... that is a different concept entirely. angular $http cache stores actual responses in javascript objects. Those are not maintained between page loads – charlietfl Aug 26 '15 at 16:01

I am not sure whether angular is able to interfere the caching of jQuery. But by default $.getScript doesn't cache.

$.getScript is a shortcut of:

$.ajax({ 
    url: '',
    dataType: "script",
    cache: false
});

Reference: http://api.jquery.com/jQuery.getScript/

By default, $.getScript() sets the cache setting to false.

You need to use:

$.ajax({
    url: url,
    cache: true,
    dataType: 'script',
    ...
});
share|improve this answer
    
This doesn't prevent another request to server. It really has no impact at all. The cache of $.ajax is not the same concept as angular cache where data is literally stored in a datastore within page – charlietfl Aug 26 '15 at 15:50
    
@charlietfl It does make change. Please check jsfiddle.net/Lttg8ava. Using cache:false makes the response code 200 every time. However, using cache:true makes subsequent response code 304, which means no more new content from server. – Joy Aug 26 '15 at 15:56
    
OK but concept is still different than angular cache. I really think OP wants to prevent this code running again. I get 200 with cache true also ...opps probably because I have cache disabled – charlietfl Aug 26 '15 at 15:58
    
from docs: Note: Setting cache to false will only work correctly with HEAD and GET requests.. The cache ajax does is just add a timestamp in url. So it doesn't even add it for script requests – charlietfl Aug 26 '15 at 16:11
    
script requests are GET requests. cache option applies to getScript. – Joy Aug 26 '15 at 16:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.