有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
这是我在一个使用PHP的应用程序中使用的简单解决方案。
所有JS和CSS文件都放在一个有版本名的文件夹中。示例:"1.0.01"
root\1.0.01\JS
root\1.0.01\CSS
在那里创建一个Helper并定义版本号
<?php
function system_version()
{
return '1.0.07';
}
和链接JS和SCC文件如下
<script src="<?= base_url(); ?>/<?= system_version();?>/js/generators.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<?= base_url(); ?>/<?= system_version(); ?>/css/view-checklist.css" />
每当我对任何JS或CSS文件进行更改时,我都会在Helper中更改System Verson并重命名文件夹并部署它。
其他回答
查看缓存控制和过期的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 " > < /脚本>
更新URL为以下工作为我:
/ custom.js吗?id = 1
通过在?id=后添加一个唯一的数字,并随着新的变化而递增,用户不必按CTRL + F5来刷新缓存。或者,您可以在?id=后附加当前时间或Epoch的哈希或字符串版本
id=1520606295
我也遇到过类似的问题,我是这样解决的:
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页面将更新缓存。
解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。
很多答案都忽略了一点——大多数开发人员都很清楚关闭缓存是低效的。然而,在许多常见的情况下,效率并不重要,默认缓存行为被严重破坏。
这些包括嵌套的、迭代的脚本测试(大的一个!)和坏的第三方软件变通方法。这里给出的解决方案都不足以解决这种常见场景。大多数web浏览器都有过于激进的缓存,并且没有提供合理的方法来避免这些问题。
<meta http-equiv="pragma" content="no-cache" />
参见https://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images