同源政策错误

问题描述:

我想了解同源政策。有一个site谈论它。但必须有某事。错与第一例子,因为我得到一个错误Illegal document.domain value同源政策错误

这里是有问题的代码:

位于http://www.qnimate.com/parent.html父站点:

<iframe src="http://www.blog.qnimate.com/child.html" id="myIFrame"></iframe> 
<script> 
window.document.domain = "www.qnimate.com";//you also need to set the parent's document.domain variable 
window.document.getElementById("myIFrame").contentWindow.document.body.style.backgroundColor = "red";//this access is allowed by default 
</script> 

和iframe位于http://www.blog.qnimate.com/child.html

<script> 
window.document.domain = "www.qnimate.com"; //if we remove this line then the below line will not work and throw a same origin policy exception. 
window.parent.document.body.style.backgroundColor = "blue"; 
</script> 

您只能将document.domain设置为当前域的超级域。您可以从左侧移除组件。

当前的域名是www.blog.qnimate.com,所以它可以设置为blog.qnimate.comqnimate.com

你不能删除中间的组件,所以你不能有www.qnimate.com


要通过iframe跨越不同来源进行通信,使用postMessage如上所述in this question

+0

好吧,所以上面的例子是不正确的? – Mulligun81

+0

@ Mulligun81 - 是的。您观察到的示例代码抛出错误是不正确的。 – Quentin