Chrome中针对document.domain的解决方案

15

出于某些原因,Chrome不再支持document.domain,并且当在包含子域的iframe中读取该行时,会在包含iframe的子域中抛出错误。是否有任何解决方法?

错误:未捕获的错误:SECURITY_ERR:DOM异常18

2个回答

19

文档域名应该使用小写字母,规则如下:

// Actual domain is "www.foo.com" 
document.domain = "foo.com"; // this is valid 

// Actual domain is "bar.foo.com" 
document.domain = "www.foo.com"; // this is invalid, "bar.foo.com" is not a subdomain of "www.foo.com" 

// Actual domain is "blah.bar.foo.com" 
document.domain = "bar.foo.com" // Ok 
document.domain = "foo.com" // Still ok 
document.domain = "bar.foo.com" // Invalid, you can't change it back to a more specific domain. 

1
以防有人需要参考:https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy - juminoz

0

document.domain 应该适用于 iframe,只要它们在同一个域名下:

iframe=document.querySelector('iframe');
console.log(iframe.contentDocument.domain)

如果您尝试访问一个与父框架不同域的iframe文档,您会看到安全错误。

请注意,子域被视为不同的域,因此您可能会遇到跨域问题:关于跨域(子域)ajax请求的问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接