如何清除iFrame的缓存?

时间:2023-02-12 19:18:01

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。我正在尝试使用按钮和javascript之类的东西。在不清除缓存的情况下,我似乎无法让iFrame重新加载。让PHP清除缓存会更好。

EDIT-UPDATE

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>

3 个解决方案

#1


15  

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头或 标记(参见参考)控制来自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);

#2


2  

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

#3


1  

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中获取参数,而不必担心是否使用?或者你的随机选择。

#1


15  

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头或 标记(参见参考)控制来自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);

#2


2  

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

#3


1  

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中获取参数,而不必担心是否使用?或者你的随机选择。