如何在浏览器中强制刷新 javascript 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8230589/
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
How do I force the refresh of javascript files in a browser?
提问by MikeD
I've been using quite a bit of JQuery, KnockoutJS and other JavaScript stuff for a web app I am building. Most of the JS is off in its own nice separate files. The issue I have is that the browsers cache those files so when I push a change people have to refresh a few times before they get the updated file. Recently I made a significant change to how some data was passed and the cached versions of the files caused some errors and worse, some data to get erased.
我一直在为我正在构建的 Web 应用程序使用相当多的 JQuery、KnockoutJS 和其他 JavaScript 东西。大多数 JS 都在它自己漂亮的单独文件中。我遇到的问题是浏览器会缓存这些文件,因此当我推送更改时,人们必须刷新几次才能获得更新的文件。最近,我对某些数据的传递方式进行了重大更改,文件的缓存版本导致了一些错误,更糟糕的是,一些数据被删除了。
How do people handle this? Is there a way to force refresh of files?
人们如何处理?有没有办法强制刷新文件?
回答by Lee
Different dev's do different things. The official way is to play nicley and use HTTP headers. Google for http heads and caching issues and you should be fine to continue on your own way.
不同的开发人员做不同的事情。官方方法是玩 nicley 并使用 HTTP 标头。谷歌搜索 http 头和缓存问题,你应该可以按照自己的方式继续。
However some browsers just ignore you so if im developing in a live environment, i use version numbers to ensure everyone gets the latest file. So you might call your original "something.js?v=1.0". Make a small change then change it to "?v=1.1".. you get the idea. Because the link is different, it should completely ignore caching in most cases.
然而,有些浏览器会忽略你,所以如果我在实时环境中开发,我会使用版本号来确保每个人都能获得最新的文件。所以你可以称你原来的“something.js?v=1.0”。做一个小改动,然后把它改成“?v=1.1”……你明白了。因为链接不同,所以在大多数情况下应该完全忽略缓存。
I tend to use both methods just to be as sure as possible
我倾向于使用这两种方法只是为了尽可能确定
回答by mattacular
The only way I know of to force all browsers to ignore cache is to add some sort of unique identifier to the script tag.
我所知道的强制所有浏览器忽略缓存的唯一方法是向脚本标记添加某种唯一标识符。
One thing you could do is add a timestamp or version number via a GET param that won't mean anything to the server, or just change the filename of the script itself if that is feasible:
您可以做的一件事是通过 GET 参数添加时间戳或版本号,这对服务器没有任何意义,或者如果可行,只需更改脚本本身的文件名:
<script type="text/javascript" src="myscript.js?version=2.0"></script>
<script type="text/javascript" src="myscript.js?ts=9342038423048"></script>
<script type="text/javascript" src="myscript.2.0.js"></script>
If significant changes have been made to the JS file though most browsers should be smart enough to fetch a fresh copy... you can also tweak your response headers if you have access to the server (cache-expire etc)
如果对 JS 文件进行了重大更改,尽管大多数浏览器应该足够智能以获取新副本……如果您有权访问服务器(缓存过期等),您还可以调整响应标头
回答by P.Brian.Mackey
The trouble is that a browser will cache when you perform a GET
request against the same URL. As others have said, you can append a timestamp as a query parameter. This forces a new GET
request as the URL has changed.
问题在于,当您GET
对同一 URL执行请求时,浏览器会缓存。正如其他人所说,您可以附加时间戳作为查询参数。这会GET
在 URL 更改时强制执行新请求。
For ajax specifically, there is a jQuery.ajax parameterthat does this for you
特别是对于 ajax,有一个 jQuery.ajax参数可以为您执行此操作
cacheBoolean Default: true, false for dataType 'script' and 'jsonp'
If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.
cacheBoolean 默认值:true,false 用于 dataType 'script' 和 'jsonp'
如果设置为 false,它将强制请求的页面不被浏览器缓存。将缓存设置为 false 还会将查询字符串参数“_=[TIMESTAMP]”附加到 URL。
When using AJAX you should use this method. Otherwise, append your own timestamp or modify the headers (which should work in modern browsers).
使用 AJAX 时,您应该使用此方法。否则,附加您自己的时间戳或修改标题(这应该适用于现代浏览器)。