How to clear iFrame cache?

I have a webpage where I am trying to update an iFrame. I am trying to do this with a button <input />and javascript. I cannot force the iFrame to reboot without clearing the cache. Getting PHP to clear the cache would be even better.

EDIT-UPDATE

Here's a working inline implementation.

    <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 the coarse download was soon consistent, eliminating the need for a 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>
+5
source share
3 answers

The first choice is probably to control browser caching for the iframe page from your web server, either using HTTP headers or with tags <meta>(see reference ).

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

, .src iframe, , - .

:

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

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

You can do something like

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

To allow GET parameters in a URL without worrying about whether to use ? or & for your random.

+1
source

All Articles