iframe后退导致主窗口返回

时间:2017-01-07 02:37:12

标签: javascript html iframe

这是我的代码:

<iframe id="ifr" src="https://www.wikipedia.org/" frameborder="0"></iframe>
<script>
  var ifr = document.querySelector("#ifr")
  ifr.src = "http://bing.com"
  ifr.contentWindow.history.back()
</script>

我希望结果是wikipedia.org,但是当我在chrome中打开一个新标签页并输入/home/roroco/Dropbox/jss/ro-js/ex/test-iframe-back.html时,它会显示“新标签页”,这意味着主窗口又回来了

enter image description here

如何避免主窗口回归

1 个答案:

答案 0 :(得分:0)

如果你有&#34;后退按钮&#34;在您的iFrame.html页面中,您必须使用:

window.history.back();

而不是:

ifr.contentWindow.history.back();

在你的html文件中,添加:

<input type="button" onClick="GoBack();">

并在您的JavaScript文件中添加:

function GoBack() {
window.history.back();
}

你可以使用:

parent.history.back();
在iFrame中返回iFrame的父级。