有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
强制浏览器清除缓存或重新加载正确的数据?我已经尝试了stackoverflow中描述的大多数解决方案,一些工作,但过了一会儿,它最终缓存并显示先前加载的脚本或文件。是否有另一种方法,将清除缓存(css, js等),并实际工作在所有浏览器?
I found so far that specific resources can be reloaded individually if you change the date and time on your files on the server. "Clearing cache" is not as easy as it should be. Instead of clearing cache on my browsers, I realized that "touching" the server files cached will actually change the date and time of the source file cached on the server (Tested on Edge, Chrome and Firefox) and most browsers will automatically download the most current fresh copy of whats on your server (code, graphics any multimedia too). I suggest you just copy the most current scripts on the server and "do the touch thing" solution before your program runs, so it will change the date of all your problem files to a most current date and time, then it downloads a fresh copy to your browser:
<?php
touch('/www/sample/file1.css');
touch('/www/sample/file2.js');
?>
然后……剩下的程序…
It took me some time to resolve this issue (as many browsers act differently to different commands, but they all check time of files and compare to your downloaded copy in your browser, if different date and time, will do the refresh), If you can't go the supposed right way, there is always another usable and better solution to it. Best Regards and happy camping. By the way touch(); or alternatives work in many programming languages inclusive in javascript bash sh php and you can include or call them in html.
其他回答
不确定这是否真的对你有帮助,但这就是缓存在任何浏览器上的工作方式。当浏览器请求一个文件时,它应该总是向服务器发送请求,除非存在“脱机”模式。服务器将读取一些参数,如日期修改或标记。
服务器将为NOT MODIFIED返回一个304错误响应,浏览器将不得不使用它的缓存。如果标签在服务器端没有验证,或者修改的日期低于当前修改的日期,服务器应该返回带有新的修改日期或标签或两者的新内容。
如果没有缓存数据发送到浏览器,我猜行为是不确定的,浏览器可能会或可能不会缓存文件,不告诉他们是如何缓存的。如果您在响应中设置了缓存参数,它将正确缓存您的文件,然后服务器可能会选择返回一个304错误,或者返回新的内容。
这是应该做的。在url中使用随机参数或版本号更像一个黑客。
http://www.checkupdown.com/status/E304.html http://en.wikipedia.org/wiki/HTTP_ETag http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/
看了之后,我看到还有一个过期日期。如果你有问题,可能是因为你设置了过期日期。换句话说,当浏览器缓存你的文件时,因为它有一个有效期,所以它不应该在这个日期之前再次请求它。换句话说,它永远不会向服务器请求文件,也永远不会收到一个未修改的304。它将简单地使用缓存,直到达到过期日期或缓存被清除为止。
所以这是我的猜测,你有某种有效期,你应该使用最后修改标签或它们的混合,并确保没有过期日期。
如果人们倾向于频繁地刷新文件,而文件不会经常更改,那么设置一个大的过期日期可能是明智的。
我的2分钱!
并非如此。一种方法是在传递内容时发送适当的头文件,以迫使浏览器重新加载:
确保网页没有在所有浏览器上缓存。
如果你在SO上搜索“缓存头”或类似的东西,你会发现ASP。NET特定的示例。
另一种不太清晰但有时是唯一的方法,如果你不能控制服务器端的头,就是给被调用的资源添加一个随机的GET参数:
myimage.gif?random=1923849839
除了设置Cache-control: no-cache,如果你想每次刷新本地副本,你还应该将Expires头设置为-1(某些版本的IE似乎需要这样做)。
查看HTTP缓存-与服务器检查,始终发送If-Modified-Since
更新2012
这是一个老问题,但我认为它需要一个更最新的答案,因为现在有一种方法可以更好地控制网站缓存。
在离线Web应用程序(实际上是任何HTML5网站)中,applicationCache.swapCache()可以用来更新网站的缓存版本,而不需要手动重新加载页面。
这是一个来自HTML5 Rocks上使用应用程序缓存的初学者指南的代码示例,解释了如何将用户更新到站点的最新版本:
// Check if a new cache is available on page load.
window.addEventListener('load', function(e) {
window.applicationCache.addEventListener('updateready', function(e) {
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
// Swap it in and reload the page to get the new hotness.
window.applicationCache.swapCache();
if (confirm('A new version of this site is available. Load it?')) {
window.location.reload();
}
} else {
// Manifest didn't changed. Nothing new to server.
}
}, false);
}, false);
有关更多信息,请参见在Mozilla开发者网络上使用应用程序缓存。
更新2016
网络上的事物变化很快。 这个问题是在2009年提出的,在2012年,我发布了一篇关于处理问题中描述的问题的新方法的更新。又过了4年,现在看来它已经被弃用了。感谢cgaldiolo在评论中指出这一点。
目前,截至2016年7月,HTML标准,第7.9节,脱机Web应用程序包括一个弃用警告:
这个特性正在从Web平台上移除。 (这是一个漫长的过程,需要很多年。)使用任何 此时非常不鼓励使用离线Web应用程序特性。 使用服务工作者代替。
在Mozilla开发者网络上使用应用程序缓存也是如此,我在2012年引用过:
已弃用该特性已从Web标准中移除。 虽然一些浏览器可能仍然支持它,但它仍在开发过程中 被删除。不要在旧的或新的项目中使用它。页面或Web应用程序 使用它可能会随时损坏。
请参见Bug 1204581 -如果启用service worker获取拦截,为AppCache添加弃用通知。
我也遇到过类似的问题,我是这样解决的:
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页面将更新缓存。
解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。