javascript 如何清除 iFrame 的缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12135544/
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 clear the cache of an iFrame?
提问by Jarrett Mattson
I have a web page in which I am trying to refresh a iFrame. I'm trying to do it with something like a <input />
button and javascript. I can't seem to get the iFrame to reload without clearing the cache. Getting PHP to clear the cache would be even better.
我有一个网页,我试图在其中刷新 iFrame。我正在尝试使用<input />
按钮和 javascript 之类的东西来做到这一点。我似乎无法在不清除缓存的情况下重新加载 iFrame。让 PHP 清除缓存会更好。
EDIT-UPDATE
编辑更新
Here's the working implementation inline.
这是内联的工作实现。
<input type="button" onClick="javascript: var iFrame = document.getElementById('compilePreview'); iFrame.src = '<? echo ($myFile); ?>?random=' + (new Date()).getTime() + Math.floor(Math.random() * 1000000);" value="Reload Preview" />
<iframe id="compilePreview" src="<? echo ($myFile); ?>" width="940"></iframe>
And of coarse the onload was soon to follow, eliminating the need for the button.
粗略的加载很快就会随之而来,消除了对按钮的需要。
<script>
window.onload=refreshIframe;
function refreshIframe(){
var iFrame = document.getElementById('compilePreview');
iFrame.src = '<? echo ($myFile); ?>?random=' + (new Date()).getTime() + Math.floor(Math.random() * 1000000);
}
</script>
回答by jfriend00
The first choice is probably to control browser caching for the iframe page from your web server either with HTTP headers or with <meta>
tags (see reference).
第一个选择可能是使用 HTTP 标头或<meta>
标签控制来自您的 Web 服务器的 iframe 页面的浏览器缓存(请参阅参考资料)。
<meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
If you can't change those, then you can set a .src
in the iframe that has a different query parameter each time to go around caching.
如果你不能改变这些,那么你可以.src
在 iframe 中设置一个每次都有不同的查询参数来绕过缓存。
For example:
例如:
iframeObj.src = "http://www.example.com/page/myframe.html?random=" + (new Date()).getTime() + Math.floor(Math.random() * 1000000);
回答by Lusitanian
This is something you should do on the server side, controlled via HTTP headers like so:
这是您应该在服务器端执行的操作,通过 HTTP 标头进行控制,如下所示:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sun, 29 Jul 2012 00:00:00 GMT"); // some day in the past
回答by Guest
You could do something like
你可以做类似的事情
<iframe src="<?php echo $url.'#nocache'.time(); ?>">
#document</iframe>
Which would allow for GET parameters in the URL without also having to worry about whether to use ?or &for your random.
哪个将允许在 URL 中使用 GET 参数而不必担心是否使用?或&为您的随机。