Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  68] [ 2]  / answers: 1 / hits: 23037  / 12 Years ago, mon, august 27, 2012, 12:00:00

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.



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>

More From » php

 Answers
5

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



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



For example:



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

[#83415] Friday, August 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;