iframe Back with javascript - avoid main page go b

2019-06-02 09:44发布

I'm using an iframe, and I have a back button which runs this javascript code (when user clicks on back button):

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.

EDIT: This is my code. (@ jsfiddle)

1条回答
仙女界的扛把子
2楼-- · 2019-06-02 10:12

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

This way you can know the "length" of history

查看更多
登录 后发表回答