简体   繁体   English

从IFrame(同一域)访问父文档给出错误

[英]Accessing parent document from IFrame (same domain) giving error

I am trying to access the parent's document from an iframe using window.parent.document , but i am getting this error - 我试图使用window.parent.document从iframe访问父文档,但我收到此错误 -

Uncaught DOMException: Blocked a frame with origin "https://abc.xyz.com" from accessing a cross-origin frame.

But the host of both parent and the iframe are the same. 但父母和iframe的主人都是一样的。 Both have the host https://abc.xyz.com 两者都有主机https://abc.xyz.com

In the iframe, I am redirecting it through a few URLs. 在iframe中,我通过几个URL重定向它。 So it starts from 所以它从...开始

https://abc.xyz.com then goes to https://abc.xyz.com然后去

https://www.facebook.com

then is redirected again to https://abc.xyz.com 然后再次重定向到https://abc.xyz.com

I am trying to access the document after this redirection, but am getting this error. 我试图在此重定向后访问该文档,但我收到此错误。 Any thoughts on why this could happen? 有关为何会发生这种情况的任何想法 Please help. 请帮忙。

You can use postMessage (Read more about it here ). 您可以使用postMessage在此处阅读更多相关信息)。

In your iframe call this: 在你的iframe中调用:

window.parent.postMessage({message: 'stuff you want to send'}, 'https://abc.xyz.com/');

In the parent window you can listen for the message event like this: 在父窗口中,您可以像这样监听消息事件:

window.addEventListener('message', function(evt) {
   if(evt.origin == 'https://abc.xyz.com/')
       console.log('Data: ' + evt.data.message);
}, false);

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

相关问题 从同一域上的父窗口访问iframe中的JavaScript变量 - Accessing JavaScript variable in an iframe, from the parent window on same domain 是否可以从父框架(相同document.domain)更改iframe的DOM? - Is it possible changing DOM of an iframe from the parent frame (same document.domain)? 从文档中访问文档的文本 <iframe> 托管在其他域上 - Accessing a document's text from an <iframe> that's hosted on a different domain 从父文档和商店访问iframe中的文本字段以供使用 - Accessing text field within iframe from parent document and store for use jQuery从iFrame和SOP访问$(parent.document).scrollTop - Jquery accessing $(parent.document).scrollTop from iFrame and SOP 如何防止iframe访问父窗口但允许父窗口访问iframe文档? - How to prevent iframe from accessing parent window but allow parent window to access iframe document? 从同域iframe向父窗口添加新函数 - Add new function to parent window from same-domain iframe 在iframe中跨网域访问时,如何从父页面访问iframe javascript变量? - How to access iframe javascript variable from parent page when accessing cross domain in iframe? 从iframe访问var到父级 - accessing var from iframe to parent 当iframe不是来自同一域时,如何强制从iframe链接在父窗口中打开? - how to force link from iframe to be opened in the parent window when iframe is not from same domain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM