tags:

views:

64

answers:

3

I'm experiencing a rather bizarre behaviour in JQuery.

I'm dynamically loading a series of scripts using this code:

for (var i=0; i<requiredScripts.length; i++)
    {
    $.ajax({
        url: baseURL+"js/"+requiredScripts[i],
        async: false,
        dataType: 'script',
        success: checkLoaded
        });
    }

The checkLoaded function just increments a counter, so that when all of the scripts have been loaded it executes certain functions.

Everything was working fine until I uploaded a newer version of one of the loaded scripts. JQuery seems to continue loading the old one. I tried refreshing (and force refreshing) the page in vain...

So I disabled the cache (from the WebDeveloper toolbar) and now the script magically loads. But when I reenabled the cache the old script came back...

I went as far as deleting the file from the server, and that did not do anything! (unless I disable the cache, that is).

I'm sure I'm just missing something really simple, but I could not figure it out.

Thanks in advance to anyone who can help.

EDIT: just to clarify, I do want the file to be cached. What I don't understand is why a force refresh of the page does not result in getting the new script.

Also, note that this only happens if I load the files with JQuery, if I add them manually in the HTML a force refresh loads the new page.

EDIT #2: solved by deleting the cache from FF. Still, don't understand why a CTRL+SHIFT+R did not do the trick...

A: 

Tell apache to sent no-cache headers for your JS files:

<Directory "/path/to/baseURL/js">
    Header Set Cache-Control "max-age=0, no-store"
</Directory>
JochenJung
Looks like bad karma to me. Caching is a good thing in general. OP should really go with adding a timestamp or even better, a release version to his `script files querystring`.
jAndy
+1  A: 

You can simply use the classic caching disabling method.

var cd=+new Date();
for (var i=0; i<requiredScripts.length; i++)
    {
        $.ajax({
            url: baseURL+"js/"+requiredScripts[i]+"?"+cd,
            async: false,
            dataType: 'script',
            success: checkLoaded
            });
    }

In this way the script url is always different and you are sure that its content is not cached.

mck89
apparently this isn't such a good idea:http://www.askapache.com/htaccess/mod_rewrite-fix-for-caching-updated-files.html
matpol
@matpol: it's not a good idea to use query strings when you want the file to be cached. What's desired here is the opposite, so this isn't a bad idea.
Andy E
it doesn't always work though and its a work around not a fix - as it won't actually fix the problem.
matpol
+2  A: 

$.ajax has a cache option that does the same thing as mck89's answer but is a little neater:

$.ajax({
    url : 'example.com',
    cache : false,
    ....
});
gustaf
This obviously work, but it is not what I need. I updated my question so that it is more clear: why can't I "overwrite" the file in cache with a force refresh of the page?
nico