Javascript 通过javascript清除浏览器缓存和历史记录并加载最新的js文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14524203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
clear browser cache and history via javascript and load latest js files
提问by Atur
how to clear browser cache and history via javascript I found some links like:-
如何通过javascript清除浏览器缓存和历史记录我发现了一些链接,如:-
How to clear browser history oan clear cache?
how to clear browser hash history in javascript
but they do not properly described on how this can be achieved via javascript ( or jQuery)
但他们没有正确描述如何通过 javascript(或 jQuery)实现这一点
I require this so that the user do not have to do a [Ctrl+F5] or manually clear cache and the most recent version of javascript file is loaded in the browser.
我需要这样做,以便用户不必执行 [Ctrl+F5] 或手动清除缓存,并且最新版本的 javascript 文件已加载到浏览器中。
The main aim is to load the latest javascript files every time the user visits the website. My application in ASP.NET MVC based and javascript files are included in .csHtml file.
主要目的是在用户每次访问网站时加载最新的 javascript 文件。我在基于 ASP.NET MVC 和 javascript 文件中的应用程序包含在 .csHtml 文件中。
回答by Avishek
call window.location.reload(true)for a ctrl+F5from script. :)
调用window.location.reload(true)了ctrl+F5从脚本。:)
回答by Lix
There are other ways to prevent a JavaScript file (or other files) from being cached.
还有其他方法可以防止 JavaScript 文件(或其他文件)被缓存。
Simply append some dummy parameters to the end of the url. For example -
只需将一些虚拟参数附加到 url 的末尾。例如 -
http://www.yourCoolSite.com/resources/js/main.js?r=SOME_RANDOM_VALUE
You would optimally do this on the server side. In PHP, this would look something like this -
您最好在服务器端执行此操作。在 PHP 中,这看起来像这样 -
echo "http://www.yourCoolSite.com/resources/js/main.js?r=".time();
Using the time()function, we append the current epoch timestamp to the URL and therefore guarantee* that the file will appear different to the browser each time.
使用该time()函数,我们将当前纪元时间戳附加到 URL 中,从而保证*该文件每次在浏览器中显示的内容都不同。

