清除 JavaScript 中的缓存 [英] Clear the cache in JavaScript

查看:28
本文介绍了清除 JavaScript 中的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 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.

如何强制浏览器重新加载缓存的 CSS/JS文件?

如何强制客户端刷新 JavaScript 文件?

动态重新加载本地 Javascript 源/json 数据

推荐答案

您可以拨打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 中的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆