火狐刷新按钮将iFrame的src改回原来的样子,但是iFrame仍然显示上一次会话的内容。

问题描述 投票:0回答:1

开始于<iframe src="blank.html" id="theIdOfTheIframe"></iframe> 其中在一个普通的html文件里面。

之后,由于用户与页面的互动 someothercontent.html 在iFrame中显示,通过改变它的 src 通过脚本来实现。

此时,如果点击Firefox浏览器的刷新按钮,iFrame的显示内容将不会恢复为 blank.html. 它仍然会显示 someothercontent.html 而控制台说 src 实际上已经回光返照,现在是 blank.html.

然而,如果我们在点击地址栏后按键盘上的ENTER键(这也是刷新页面的另一种方式),就会达到预期的效果,而iFrame实际上又是一片空白。

为了让刷新按钮发挥它应有的作用,我尝试了 contentWindow.location.reload(true); 强制在iFrame上进行 "硬刷新 "之类的操作,但这并不能改变什么,也不能工作。有什么好办法吗?

注意:在Chrome上没有这样的问题。

javascript iframe cross-browser
1个回答
0
投票

我这样做了,成功了。

const iFrameScriptAccess = document.getElementById('theIdOfTheIframe');
window.addEventListener("load",function() { // Since this will fire every time the refresh button is clicked or just F5.
  let whatTheFileNameInFrameSrcIs = iFrameScriptAccess.src.substring(iFrameScriptAccess.src.length - 10, iFrameScriptAccess.src.length-5); // Get the name of the html file without folder name and file extension.
  if (whatTheFileNameInFrameSrcIs == "blank") { // This is the moment when Firefox is giving a false positive to you and you want to make it actually show the blank.html file and not something else.
    setTimeout( function ()  {   iFrameScriptAccess.src="whateverfolder/blank.html"  },100); // Force empty! And finally things are as they are supposed to be.
  }
 });
© www.soinside.com 2019 - 2024. All rights reserved.