跨域JavaScript,嵌入iframe案例中的父位置设置

时间:2022-08-23 10:19:59

There is a solution called "Fragment Id Messaging" to solve cross domain communication by setting parent.location with extra texts followed by "#" from child iframe, it works. However, if the parent itself is embedded as an iframe in another page, parent.location setting seems denied by browsers... Is this true? If yes, is there a work-around?

有一个名为“Fragment Id Messaging”的解决方案,通过设置parent.location和其他文本,后跟子iframe中的“#”来解决跨域通信,它可以工作。但是,如果父本身作为iframe嵌入到另一个页面中,则浏览器会拒绝parent.location设置......这是真的吗?如果是,是否有解决方法?

Thanks!

2 个解决方案

#1


Which browsers are you encountering a problem, and what's the exact error message you see?

您遇到问题的浏览器是什么,您看到的确切错误消息是什么?

Generally, FIM should work correctly even in the scenario you describe, and many major websites use it.

通常,即使在您描述的场景中,FIM也应该正常工作,并且许多主要网站都使用它。

FIM should only be used when the HTML5 postMessage API (supported by IE8, FF3, Chrome, and Opera 10) is not available, as postMessage is more secure and more reliable.

仅当HTML5 postMessage API(由IE8,FF3,Chrome和Opera 10支持)不可用时才应使用FIM,因为postMessage更安全,更可靠。

#2


Modern browsers which support the postMessage API have increased security which blocks this kind of intra-frame communication. On browsers that don't have this support, the technique you're using should be fine. The only exception to this is Safari 3.2, does not support the HTML 5 postMessage API and will not allow you to set parent.location in certain circumstances.

支持postMessage API的现代浏览器增强了安全性,阻止了这种帧内通信。在没有此支持的浏览器上,您使用的技术应该没问题。唯一的例外是Safari 3.2,它不支持HTML 5 postMessage API,并且在某些情况下不允许您设置parent.location。

The other gotcha is that while IE8 has implemented the postMessage API, it has (somewhat predictably) done it differently from all the other browsers (FF3+, Chrome, Safari 4+ and Opera 9.64+). With IE8, postMessage communication between a popup and it's opener is not allowed, meaning you need to use an extra iframe for IE8 that is not required for any other browser.

另一个问题是,虽然IE8已经实现了postMessage API,但它(有些可预见地)与所有其他浏览器(FF3 +,Chrome,Safari 4+和Opera 9.64+)的完成方式不同。使用IE8,不允许在弹出窗口和它的开启者之间进行postMessage通信,这意味着您需要为IE8使用额外的iframe,这是任何其他浏览器都不需要的。

The end result is that, to implement communication between a page and popup window on a remote domain, you need one branch for decent modern browsers, one for older browsers, and a separate one each for IE8, IE7 and IE6. Thanks Microsoft...

最终结果是,为了实现远程域上的页面和弹出窗口之间的通信,您需要一个适合现代浏览器的分支,一个用于旧浏览器,另一个用于IE8,IE7和IE6。谢谢微软......

祝你好運 :)

#1


Which browsers are you encountering a problem, and what's the exact error message you see?

您遇到问题的浏览器是什么,您看到的确切错误消息是什么?

Generally, FIM should work correctly even in the scenario you describe, and many major websites use it.

通常,即使在您描述的场景中,FIM也应该正常工作,并且许多主要网站都使用它。

FIM should only be used when the HTML5 postMessage API (supported by IE8, FF3, Chrome, and Opera 10) is not available, as postMessage is more secure and more reliable.

仅当HTML5 postMessage API(由IE8,FF3,Chrome和Opera 10支持)不可用时才应使用FIM,因为postMessage更安全,更可靠。

#2


Modern browsers which support the postMessage API have increased security which blocks this kind of intra-frame communication. On browsers that don't have this support, the technique you're using should be fine. The only exception to this is Safari 3.2, does not support the HTML 5 postMessage API and will not allow you to set parent.location in certain circumstances.

支持postMessage API的现代浏览器增强了安全性,阻止了这种帧内通信。在没有此支持的浏览器上,您使用的技术应该没问题。唯一的例外是Safari 3.2,它不支持HTML 5 postMessage API,并且在某些情况下不允许您设置parent.location。

The other gotcha is that while IE8 has implemented the postMessage API, it has (somewhat predictably) done it differently from all the other browsers (FF3+, Chrome, Safari 4+ and Opera 9.64+). With IE8, postMessage communication between a popup and it's opener is not allowed, meaning you need to use an extra iframe for IE8 that is not required for any other browser.

另一个问题是,虽然IE8已经实现了postMessage API,但它(有些可预见地)与所有其他浏览器(FF3 +,Chrome,Safari 4+和Opera 9.64+)的完成方式不同。使用IE8,不允许在弹出窗口和它的开启者之间进行postMessage通信,这意味着您需要为IE8使用额外的iframe,这是任何其他浏览器都不需要的。

The end result is that, to implement communication between a page and popup window on a remote domain, you need one branch for decent modern browsers, one for older browsers, and a separate one each for IE8, IE7 and IE6. Thanks Microsoft...

最终结果是,为了实现远程域上的页面和弹出窗口之间的通信,您需要一个适合现代浏览器的分支,一个用于旧浏览器,另一个用于IE8,IE7和IE6。谢谢微软......

祝你好運 :)