简体   繁体   English

iframe用javascript返回 - 避免主页返回

[英]iframe Back with javascript - avoid main page go back

I'm using an iframe , and I have a back button which runs this javascript code (when user clicks on back button): 我正在使用iframe ,并且我有一个运行此javascript代码的后退按钮(当用户点击后退按钮时):

document.getElementById("myframe").contentWindow.history.go(-1); // back

This works fine, but If there is no previous page in the frame, main page will go back. 这工作正常,但如果框架中没有上一页,主页面将返回。

How to avoid this? 怎么避免这个? I want just iframe to go back, and if there is no previous page, simply do nothing. 我想要iframe回去,如果没有上一页,就什么都不做。

EDIT: This is my code. 编辑: 是我的代码。 (@ jsfiddle) (@ jsfiddle)

Keep track of changes like this: 跟踪这样的变化:

<script>
    window.iFrameChanges = -1; //will get incremented to 0 on first page load
</script>

<iframe onLoad="window.iFrameChanges+=1;" src="..."></iframe>

And the decrement the window.iFrameChanges value on history go back 并且减少历史记录中window.iFrameChanges

This way you can know the "length" of history 这样你就可以知道历史的“长度”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM