有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?

使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。


当前回答

我也遇到过类似的问题,我是这样解决的:

In index.html file I've added manifest: <html manifest="cache.manifest"> In <head> section included script updating the cache: <script type="text/javascript" src="update_cache.js"></script> In <body> section I've inserted onload function: <body onload="checkForUpdate()"> In cache.manifest I've put all files I want to cache. It is important now that it works in my case (Apache) just by updating each time the "version" comment. It is also an option to name files with "?ver=001" or something at the end of name but it's not needed. Changing just # version 1.01 triggers cache update event. CACHE MANIFEST # version 1.01 style.css imgs/logo.png #all other files It's important to include 1., 2. and 3. points only in index.html. Otherwise GET http://foo.bar/resource.ext net::ERR_FAILED occurs because every "child" file tries to cache the page while the page is already cached. In update_cache.js file I've put this code: function checkForUpdate() { if (window.applicationCache != undefined && window.applicationCache != null) { window.applicationCache.addEventListener('updateready', updateApplication); } } function updateApplication(event) { if (window.applicationCache.status != 4) return; window.applicationCache.removeEventListener('updateready', updateApplication); window.applicationCache.swapCache(); window.location.reload(); }

现在你只需更改文件,在manifest中你必须更新版本注释。现在访问index.html页面将更新缓存。

解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。

其他回答

您是想清除缓存,还是只是确保当前(已更改的?)页面没有缓存?

如果是后者,那就应该如此简单

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

更新URL为以下工作为我:

/ custom.js吗?id = 1

通过在?id=后添加一个唯一的数字,并随着新的变化而递增,用户不必按CTRL + F5来刷新缓存。或者,您可以在?id=后附加当前时间或Epoch的哈希或字符串版本

id=1520606295

我实现了这个简单的解决方案(还没有在生产环境中):

function verificarNovaVersio() {
    var sVersio = localStorage['gcf_versio'+ location.pathname] || 'v00.0.0000';
    $.ajax({
        url: "./versio.txt"
        , dataType: 'text'
        , cache: false
        , contentType: false
        , processData: false
        , type: 'post'
     }).done(function(sVersioFitxer) {
        console.log('Versió App: '+ sVersioFitxer +', Versió Caché: '+ sVersio);
        if (sVersio < (sVersioFitxer || 'v00.0.0000')) {
            localStorage['gcf_versio'+ location.pathname] = sVersioFitxer;
            location.reload(true);
        }
    });
}

我有一个小文件位于html的位置:

“versio.txt”:

v00.5.0014

这个函数在我的所有页面中都被调用,因此在加载时它会检查localStorage的版本值是否低于当前版本,并执行a

location.reload(true);

...强制从服务器而不是缓存重新加载。

(显然,您可以使用cookie或其他持久客户端存储而不是localStorage)

我选择这个解决方案是因为它很简单,因为只维护一个“version .txt”文件就会强制重载整个站点。

queryString方法很难实现,而且也是缓存的(如果您从v1.1更改到以前的版本将从缓存加载,那么这意味着缓存不会刷新,将所有以前的版本保存在缓存中)。

我是一个小新手,我很感激你的专业检查和审查,以确保我的方法是一个很好的方法。

希望能有所帮助。

查看缓存控制和过期的META标签。

< meta http-equiv =" cache-control " content =" no-cache "> <META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 july 2002 11:12:01 GMT">

另一个常见的做法是在请求文件的末尾附加不断变化的字符串。例如:

<脚本类型=“文本/javascript”“src=”主。js?v = 12392823 " > < /脚本>

我有同样的问题,我所做的就是改变文件名链接到我的index.html文件,然后进入index.html文件,更新他们的名字,不是最好的做法,但如果它工作。浏览器将它们视为新文件,因此它们会被重新下载到用户的设备上。

例子: 我想更新一个css文件,它的命名为styles.css,把它改成styles.css

进入index。html进行更新,并将其更改为