Html 当你只控制iframe页面(而不是框架页面)时,如何让链接脱离iframe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1060294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:03:13  来源:igfitidea点击:

How to make links break out of iframe when you only control the iframe page (not the framed pages)?

htmliframehyperlinktargetbreak

提问by Quentin

I have a site that displays other sites through an iframe on certain pages. It does this in order to display a toolbar that is relevant to the reader (like Facebook and Owly does). However, when the user chooses to leave the original site I want the bar to go away. As you might gather, I don't control the sites in the iframe, and they're on another domain than the iframing page.

我有一个网站,通过特定页面上的 iframe 显示其他网站。它这样做是为了显示与读者相关的工具栏(如 Facebook 和 Owly 所做的那样)。但是,当用户选择离开原始站点时,我希望该栏消失。正如您可能收集到的,我不控制 iframe 中的站点,它们位于 iframe 页面之外的另一个域中。

I tried target="_parent" on the <iframe>, no luck. Then I tried various scripting solutions, but they all fail due to the same domain restriction. My last try was to have a timeout check for changes in the iframe URL, but iframe.contentWindow.location.href is restricted when page is on another domain (unlike the object iframe.contentWindow.location which I found a bit weird).

我在 上尝试了 target="_parent" ,但<iframe>没有运气。然后我尝试了各种脚本解决方案,但由于相同的域限制,它们都失败了。我的最后一次尝试是对 iframe URL 中的更改进行超时检查,但是当页面位于另一个域上时 iframe.contentWindow.location.href 受到限制(与我发现有点奇怪的对象 iframe.contentWindow.location 不同)。

Any solutions to this problem? I know iframes aren't the hottest thing around, but they do the job in this case.

这个问题有什么解决方案吗?我知道 iframe 并不是最热门的东西,但在这种情况下它们可以胜任。

回答by MystikSpiral

Try target=_topThat should open the page in the full body of the window.

试试target=_top这应该会在整个窗口中打开页面。

回答by Quentin

No solutions.

没有解决方案。

The only way to get a link to open in the top frame is to have access to the link itself. As you observed, the same origin policy prevents JS from outside the frame accessing it.

在顶部框架中打开链接的唯一方法是访问链接本身。正如您所观察到的,同源策略会阻止 JS 从框架外部访问它。