简体   繁体   English

如何清除iFrame的缓存?

[英]How do I clear the cache of an iFrame?

I have a web page in which I am trying to refresh a iFrame. 我有一个网页,我正在尝试刷新iFrame。 I'm trying to do it with something like a <input /> button and javascript. 我正试图用<input />按钮和javascript之类的东西来做。 I can't seem to get the iFrame to reload without clearing the cache. 我似乎无法在不清除缓存的情况下重新加载iFrame。 Getting PHP to clear the cache would be even better. 让PHP清除缓存会更好。

EDIT-UPDATE 编辑-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. 而且很快就会出现onload,无需使用按钮。

    <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>

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. 如果你不能改变它们,那么你可以在iframe中设置一个.src ,每次都有一个不同的查询参数来绕过缓存。

For example: 例如:

iframeObj.src = "http://www.example.com/page/myframe.html?random=" + (new Date()).getTime() + Math.floor(Math.random() * 1000000);

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

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 ? 哪个允许在URL中使用GET参数而不必担心是否使用 or & for your random. 为您的随机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM