Add timestamp to src in iframe using javascript (prevent cache)
I want to prevent my iframe from being cac开发者_JAVA百科hed. The first page isn't a problem. Then I use PHP like this:
<iframe id="my_iframe" src="index.html#<?php echo time() ?>"></iframe>
The problem I have is to prevent chaching when link is clicked inside the iframe. eg.:
<a href="newpage.html">this page will be cached</a>
How can I prevent cache of the "newpage.html"? Can I somehow add timestamp to the src using java script?
yes you can do that,
var linksList = document.getElementsByTagName('a');
for( var i=0,len=linksList.length;i<len;i++ )
linksList [i].href += '#'+new Date();
this script should be placed before the closing of the body tag, or it should be generally be called after the DOM has loaded.
Now, isn't there a backend way (with headers) to ask the browser not to cache some specific pages?
Use this code in the page header whose data you don't want to cache.
<?php
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
精彩评论