简体   繁体   English

如何从iframe导航到另一个页面?

[英]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: 什么是跨平台的好方法:

  1. Detect if I'm contained in an iframe 检测我是否包含在iframe中
  2. If not, navigate like above. 如果没有,请像上面一样导航。
  3. If yes, navigate the iframe only? 如果是,仅浏览iframe?

(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.

相关问题 我如何在反应中导航到另一个页面? - How can I navigate to another page in react? 如何使用VueJS导航到另一个页面/ URL? - How can I use VueJS to navigate to another page/URL? 检测到iFrame时如何重定向到另一个页面? - How can I redirect to another page when an iFrame is detected? 如何从cshtml页面导航到webform? - How can I navigate from a cshtml page to a webform? 如何从另一个页面导航到一个元素? - How to navigate to an element from another page? 如何从另一个页面 onclick 更改 iframe 的“src”? - How do I change 'src' of an iframe from another page onclick? 如何从iframe中读取父页面的页面标题? - How can I read the page title of the parent page from an iframe? 有什么方法可以代替JS hack,可以将iframe从iframe发布到iframe之外的其他页面? - Is there any way instead of a JS hack where I can post from an iframe to another page outside the iframe? 有什么方法可以让我从另一个页面(React-Native)导航到带有参数的标记? - Is there any way that I can navigate to a marker with params from another page (React-Native)? JavaScript - 如何从主页面执行iframe的功能? - JavaScript - how can i execute a function of iframe, from main page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM