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

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


当前回答

如果这是关于.css和.js的更改,那么一种方法是通过在每个版本的文件名后面附加类似“_versionNo”的东西来“缓存破坏”。例如:

script_1.0.css // This is the URL for release 1.0
script_1.1.css // This is the URL for release 1.1
script_1.2.css // etc.

或者在文件名后面:

script.css?v=1.0 // This is the URL for release 1.0
script.css?v=1.1 // This is the URL for release 1.1
script.css?v=1.2 // etc.

您可以检查这个链接,看看它是如何工作的。

其他回答

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

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页面将更新缓存。

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

有一个小技巧可以使用。诀窍是在脚本标记中向文件名附加一个参数/字符串,并在文件更改时更改它。

<script src=“myfile.js?version=1.0.0”></script>

浏览器将整个字符串解释为文件路径,即使“?”后面的是参数。所以现在发生的是,下次当你更新你的文件时,只要改变你网站上script标签中的数字(示例<script src="myfile.js?version=1.0.1"></script>),每个用户的浏览器都会看到文件已经改变,并抓取一个新的副本。

强制浏览器清除缓存或重新加载正确的数据?我已经尝试了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.

更新URL为以下工作为我:

/ custom.js吗?id = 1

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

id=1520606295

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

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

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