jQuery和脚本缓存 [英] JQuery and script caching

查看:67
本文介绍了jQuery和脚本缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JQuery中遇到了一种奇怪的行为.

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
        });
    }

checkLoaded函数只是增加一个计数器,因此,在加载所有脚本后,它会执行某些功能.

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

一切正常,直到我上传了一个已加载脚本的较新版本. jQuery似乎会继续加载旧的.我试图刷新(并强制刷新)页面是徒劳的...

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...

所以我禁用了缓存(从WebDeveloper工具栏上),现在脚本神奇地加载了. 但是当我重新启用缓存时,旧脚本又回来了……

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.

此外,请注意,仅当我使用JQuery加载文件时才会发生这种情况,如果我在HTML中手动添加了文件,则强制刷新会加载新页面.

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 :通过从FF中删除缓存来解决.仍然,不明白为什么CTRL + SHIFT + R不能解决问题……

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

推荐答案

$.ajax具有一个缓存选项,该选项与mck89的回答相同,但是更加整洁:

$.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,
    ....
});

这篇关于jQuery和脚本缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆