javascript iframe中的内容提交后需要跳出iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6158903/
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-10-25 19:41:58  来源:igfitidea点击:

Need to break out of iframe after content in iframe is submitted

javascripthtmliframe

提问by SoLoGHoST

Ok, I am using an iframe on a page. The content within the iframe I have no control over and is being utilized with an Adobe Flash program.

好的,我在页面上使用 iframe。iframe 中的内容我无法控制并且正在与 Adob​​e Flash 程序一起使用。

But once the form is sumitted within the iframe (again which I have no control over), it loads up a PAGE, I DO HAVE CONTROL Over, but has that page within the IFRAME instead of it breaking out of the iframe on that page. How do I break out of the iframe on that page only??

但是一旦在 iframe 中提交了表单(同样我无法控制),它就会加载一个 PAGE,我确实可以控制,但是在 IFRAME 中拥有该页面,而不是从该页面上的 iframe 中分离出来。如何仅在该页面上跳出 iframe?

I'm assuming that some Javascript is needed, but this code is no good:

我假设需要一些 Javascript,但这段代码不好:

if (top.location != self.location) {
top.location = self.location;
}

Using this code loads up the main index page for the site. I need it to just load the page after the form is submitted and break out of it. Again, I have access to the page that gets loaded within the iframe after the form gets submitted within the iframe.

使用此代码加载站点的主索引页面。我需要它在提交表单后加载页面并退出它。同样,在 iframe 中提交表单后,我可以访问在 iframe 中加载的页面。

Please help me.

请帮我。

Perhaps a tricky way of doing it is involved? For example while the page is being loaded? Or somehow check that the iframe has been submitted? I control both pages on my website (The page that holds the iframe originally and the page that the iframe gets submitted to), just not the iframes content.

也许涉及一种棘手的方法?例如,在加载页面时?或者以某种方式检查 iframe 是否已提交?我控制我网站上的两个页面(最初包含 iframe 的页面和 iframe 提交到的页面),而不是 iframes 内容。

Thanks :)

谢谢 :)

采纳答案by scunliffe

I believe you want this

我相信你想要这个

if(this != top){
  top.location.href = this.location.href;
}

To break out

寻求突破

It might need the document reference too... I'm not at a computer to check.

它可能也需要文档参考...我不在电脑前检查。

if(this != top){
  top.document.location.href = this.document.location.href;
}

回答by Ryan

What worked for me was:

对我有用的是:

(function () {
    'use strict';
    console.log('window.top.location', window.top.location);
    console.log('window.location', window.location);

    if (window.location !== window.top.location) {
        window.top.location = window.location;
    }
})();

Inspired by https://css-tricks.com/snippets/javascript/break-out-of-iframe/

灵感来自https://css-tricks.com/snippets/javascript/break-out-of-iframe/

回答by Mark Meuer

Chris Coyier at css-tricks.comhas a nice succinct explanation of how to do this.

css-tricks.com 的Chris Coyier对如何做到这一点有一个很好的简洁解释。

One way is a little clearer to the observer (as much as production Javascript code can ever said to be "clear"):

一种方法对观察者来说更清晰一些(就像生产 Javascript 代码可以说是“清晰的”一样):

(function(window) {
  if (window.location !== window.top.location) {
    window.top.location = window.location;
  }
})(this);

The other is much shorter, but also trickier and less obvious:

另一个更短,但也更棘手且不那么明显:

this.top.location !== this.location && (this.top.location = this.location);

Again, credit where credit is due: I didn't write these snippets, I'm just passing them along because they answer the question.

再次强调,应归功于:我没有写这些片段,我只是传递它们,因为它们回答了问题。