问题描述
如何使用 JavaScript 清除浏览器缓存?
How do I clear a browsers cache with JavaScript?
我们部署了最新的 JavaScript 代码,但无法获取最新的 JavaScript 代码.
We deployed the latest JavaScript code but we are unable to get the latest JavaScript code.
编者注:此题在以下地方半重复,以下第一题的答案可能是最好的.这个被接受的答案不再是理想的解决方案.
Editorial Note: This question is semi-duplicated in the following places, and the answer in the first of the following questions is probably the best. This accepted answer is no longer the ideal solution.
推荐答案
您可以拨打window.location.reload(true) 重新加载当前页面.它将忽略任何缓存的项目并从服务器检索页面、CSS、图像、JavaScript 等的新副本.这不会清除整个缓存,但具有清除您所在页面的缓存的效果.
You can call window.location.reload(true) to reload the current page. It will ignore any cached items and retrieve new copies of the page, css, images, JavaScript, etc from the server. This doesn't clear the whole cache, but has the effect of clearing the cache for the page you are on.
但是,您最好的策略是按照其他各种答案中提到的方式对路径或文件名进行版本控制.此外,请参见 Revving Filenames: don出于不使用 ?v=n
作为版本控制方案的原因,请使用查询字符串.
However, your best strategy is to version the path or filename as mentioned in various other answers. In addition, see Revving Filenames: don’t use querystring for reasons not to use ?v=n
as your versioning scheme.
这篇关于清除 JavaScript 中的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!