html - iframe Back with javascript - avoid main page go back - Stack Overflow

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

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)

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)

Share Improve this question edited Jul 22, 2012 at 17:36 Mahdi Ghiasi asked Jul 22, 2012 at 17:17 Mahdi GhiasiMahdi Ghiasi 15.3k19 gold badges77 silver badges121 bronze badges 5
  • Maybe check iframes 'location.href' property? If it's default - do nothing. – sleepwalker Commented Jul 22, 2012 at 17:37
  • @sleepwalker I haven't tested it, it seems to work fine. But... Isn't there any better way?? – Mahdi Ghiasi Commented Jul 22, 2012 at 17:42
  • I'm not using plete address. (I'm using ../myfolder/a.html), and even checking iframe.src didn't work. – Mahdi Ghiasi Commented Jul 22, 2012 at 17:45
  • What do you mean about checking location.href?? what is location.href? – Mahdi Ghiasi Commented Jul 22, 2012 at 17:45
  • document.getElementById('myframe').contentWindow.location.href It will work only if you are on the same domain. – sleepwalker Commented Jul 22, 2012 at 18:20
Add a ment  | 

1 Answer 1

Reset to default 3

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302063a4621492.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信