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

查看:157
本文介绍了在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:不要使用查询字符串的原因是不使用?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天全站免登陆