最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论