[英]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.