有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
我有同样的问题,我所做的就是改变文件名链接到我的index.html文件,然后进入index.html文件,更新他们的名字,不是最好的做法,但如果它工作。浏览器将它们视为新文件,因此它们会被重新下载到用户的设备上。
例子: 我想更新一个css文件,它的命名为styles.css,把它改成styles.css
进入index。html进行更新,并将其更改为
其他回答
查看缓存控制和过期的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 " > < /脚本>
并非如此。一种方法是在传递内容时发送适当的头文件,以迫使浏览器重新加载:
确保网页没有在所有浏览器上缓存。
如果你在SO上搜索“缓存头”或类似的东西,你会发现ASP。NET特定的示例。
另一种不太清晰但有时是唯一的方法,如果你不能控制服务器端的头,就是给被调用的资源添加一个随机的GET参数:
myimage.gif?random=1923849839
<meta http-equiv="pragma" content="no-cache" />
参见https://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images
我也遇到过类似的问题,我是这样解决的:
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页面将更新缓存。
解决方案的部分不是我的,但我通过互联网找到了它们,并把它们放在一起,所以它是有效的。
对于静态资源,正确的缓存是使用带有每个部署或文件版本值的查询参数。这将在每次部署后清除缓存。
/Content/css/Site.css?version={FileVersionNumber}
这是ASP。NET MVC的例子。
<link href="@Url.Content("~/Content/Css/Reset.css")?version=@this.GetType().Assembly.GetName().Version" rel="stylesheet" type="text/css" />
不要忘记更新程序集版本。