简体   繁体   English

在外部域上跨iFrame postMessage

[英]Cross iFrame postMessage on external domain

I've got multiple iFrames (same domain) on an external domain. 我在外部域上有多个iFrame(相同域)。 If I do window.top.frames["name-here"].postMessage() and the frame exists, then obviously the postMessage executes correctly. 如果我执行window.top.frames [“ name-here”]。postMessage()并且框架存在,则显然postMessage可以正确执行。

However, In this case the iFrames are modular based so they all do not need to exists. 但是,在这种情况下,iFrame是基于模块的,因此不需要全部存在。 So if I init "window.top.frames["name-here"].postMessage("HI","*")" and that frame doesn't exists, it'll incur a Blocked a frame with origin "xyz" from accessing a cross-origin frame. 因此,如果我初始化“ window.top.frames [” name-here“]。postMessage(” HI“,” *“)”且该框架不存在,则会引发一个阻止源为“ xyz”的框架访问跨域框架。 I'm assuming this isn't a big deal since it won't throw any on page errors. 我假设这没什么大不了的,因为它不会引发任何页面错误。

I've tried looping through the window.top.frames with postMessage, but that also throws a CORS error. 我试图用postMessage循环遍历window.top.frames,但这也会引发CORS错误。

The error is probably not with the postMessage , but with the access to the frame itself. 该错误可能与postMessage无关,而与对框架本身的访问有关。 A call to window.top.frames["name-here"] will most likely give you the same message. 调用window.top.frames["name-here"]很可能会给您同样的消息。 As you say, I don't think the error is a big problem, but if you want to get rid of it, you can get frames length and send your messages accordingly. 如您所说,我认为错误不是大问题,但是如果您想消除它,则可以获取frames并相应地发送消息。 Like this: 像这样:

var existing_frames = window.top.frames.length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM