javascript window.open 缓存问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3627163/
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
window.open cache problem
提问by Mouadh
I'm using window.openin a function to open a web page, which displays help for the current page.
我在一个函数中使用window.open打开一个网页,该网页显示当前页面的帮助。
The pages URL are stored in DB , some pages need authentication.
页面 URL 存储在 DB 中,有些页面需要身份验证。
For these pages, the first time we call them the user has to authenticate, but if he closes the page and opens it another time , it's the cached page that is displayed.
对于这些页面,我们第一次调用它们时用户必须进行身份验证,但是如果他关闭页面并再次打开它,则会显示缓存页面。
I've try to add the time to the url, in order to not display the cached page
我尝试将时间添加到 url,以便不显示缓存页面
var oDate = new Date();
window.open(url+oDate.getTime());
But the browser is still displaying the cached url.
但是浏览器仍然显示缓存的 url。
Any idea to resolve this problem?
任何想法来解决这个问题?
Thanks.
谢谢。
回答by Griff
You may want to try explicitly adding meta tags to your page:
您可能想尝试将元标记显式添加到您的页面:
<!-- HTTP 1.1 -->
<meta http-equiv="Cache-Control" content="no-store"/>
<!-- HTTP 1.0 -->
<meta http-equiv="Pragma" content="no-cache"/>
<!-- Prevents caching at the Proxy Server -->
<meta http-equiv="Expires" content="0"/>
回答by Vishwani
Set clearcache or clearsessioncache equal to yes as required:
根据需要将 clearcache 或 clearsessioncache 设置为 yes :
window.open(url, '_blank', 'location=yes', 'clearcache=yes');
window.open(url, '_blank', 'location=yes', 'clearcache=yes');
回答by Massimo Sedda
Try this, if you want to open a new page, without caching problems... it seems to work for me:
试试这个,如果你想打开一个新页面,没有缓存问题......它似乎对我有用:
function openGoogleLinkWithDummy()
{
var randNumber = Math.floor(Math.random()*99);
var str="How are you doing today? " + randNumber;
window.open("http://www.google.com?q=cat&" + randNumber);
}
It surely works if you want to load a external javascript file (from server side code, at example, you append a random number to the name of the zzz.js file and it seems like zzz.js?v=123) but the trick will be ok for your problem, too.
如果您想加载外部 javascript 文件(例如,从服务器端代码,您在 zzz.js 文件的名称后附加一个随机数,它看起来像 zzz.js?v=123),它肯定可以工作,但是技巧也可以解决您的问题。
The browser sees a different "version" every time you click the link because of the dummy number appended to the end.
每次单击链接时,浏览器都会看到不同的“版本”,因为末尾附加了虚拟数字。
Bye!
再见!

