使用javascript将时间戳添加到iframe中的src(防止缓存)

Add timestamp to src in iframe using javascript (prevent cache)

本文关键字:src 缓存 中的 iframe javascript 时间戳 添加 使用      更新时间:2023-09-26

我想阻止我的iframe被缓存。第一页不是问题。然后我使用这样的PHP:

<iframe id="my_iframe" src="index.html#<?php echo time() ?>"></iframe>

我遇到的问题是,当在iframe中单击链接时,要防止chaching。例如:

<a href="newpage.html">this page will be cached</a>

如何防止缓存"newpage.html"?我可以使用java脚本以某种方式将时间戳添加到src中吗?

是的,你可以做到,

var linksList = document.getElementsByTagName('a');
for( var i=0,len=linksList.length;i<len;i++ )
    linksList [i].href += '#'+new Date();

这个脚本应该放在body标记关闭之前,或者通常应该在加载DOM之后调用它。

现在,是否有一种后端方式(使用标头)可以要求浏览器不要缓存某些特定页面?

在不想缓存其数据的页头中使用此代码。

<?php
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>