[英]How can I navigate to another page, from an iframe?
I have a page with one button. 我有一个带有一个按钮的页面。 When clicked, that button navigates to http://google.com/
单击后,该按钮导航到http://google.com/
$("#button").click(function(){
window.location="http://google.com";
});
I would like this navigation to work when this page is embedded within the iframe. 当此页面嵌入到iframe中时,我希望此导航有效。 I don't want to affect the outside host page, but rather only the contents of the iframe.
我不想影响外部宿主页面,而只影响iframe的内容。 What's a good cross-platform way to:
什么是跨平台的好方法:
(I'm going to try to implement the algorithm I just described, but regardless I think this question is interesting enough to be posted. If I succeed, I'll post my solution) (我将尝试实现刚才描述的算法,但是无论我认为这个问题是否足够有趣,都可以发布。如果成功,我将发布解决方案)
1) Detect if I'm contained in an iframe
1)检测我是否包含在iframe中
if (window != window.top) {
// the page is inside an iframe
}
2) If not, navigate like above.
2)如果没有,请像上面一样导航。
navigate like above 像上面一样导航
3) If yes, navigate the iframe only?
3)如果是,仅浏览iframe?
When you write window.location.href = 'http://www.google.com';
当您编写
window.location.href = 'http://www.google.com';
you are navigating the contents of the iframe, not that of the top page. 您正在浏览iframe的内容,而不是首页的内容。 If you want to navigate the top page, you can only do this if this top page is on the same domain as the iframe and you could use
window.top.location.href
. 如果要导航首页,则仅当该首页与iframe处于同一域并且可以使用
window.top.location.href
,才能执行此window.top.location.href
。
UPDATE: 更新:
There is a security mechanism built in browsers which forbid you from redirecting to sites that set the X-Frame-Options: SAMEORIGIN
response header when inside an iframe. 浏览器中内置了一种安全机制,该机制禁止您在iframe内重定向到设置
X-Frame-Options: SAMEORIGIN
响应标头的网站。 That's the case with http://www.google.com
. http://www.google.com
就是这种情况。 Simply navigate to this site and look at the response HTTP headers with FireBug or developer toolbar you are using and you will see this header. 只需导航至该站点,并使用FireBug或正在使用的开发人员工具栏查看响应HTTP标头,即可看到此标头。 You cannot redirect to it and you will get the following error message:
您不能重定向到它,并且您会收到以下错误消息:
Refused to display document because display forbidden by X-Frame-Options.
It's basically a security mechanism implemented by some sites whose authors didn't want you to embed them in an iframe. 这基本上是由某些网站的作者实施的安全机制,这些网站的作者不希望您将其嵌入到iframe中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.