如何清除iFrame的缓存?

时间:2012-08-27 02:15:54

标签: php javascript html

我有一个网页,我正在尝试刷新iFrame。我正试图用<input />按钮和javascript之类的东西来做。我似乎无法在不清除缓存的情况下重新加载iFrame。让PHP清除缓存会更好。

EDIT-UPDATE

这是内联的工作实现。

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

粗略的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>

3 个答案:

答案 0 :(得分:16)

第一种选择可能是使用HTTP标头或<meta>标记从您的网络服务器控制iframe页面的浏览器缓存(请参阅reference)。

<meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

如果您无法更改这些内容,则可以在iframe中设置.src,每次都有一个不同的查询参数来进行缓存。

例如:

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

答案 1 :(得分:2)

这是您应该在服务器端执行的操作,通过HTTP标头进行控制,如下所示:

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sun, 29 Jul 2012 00:00:00 GMT"); // some day in the past

答案 2 :(得分:1)

您可以执行类似

的操作
<iframe src="<?php echo $url.'#nocache'.time(); ?>">
#document</iframe>

允许在网址中使用GET参数,而不必担心是否对随机使用&amp;