由于某些原因,Chrome不再支持document.domain,并且在包含子域的iframe和包含iframe的子域中读取行时会发出错误。反正有吗?
错误:未捕获错误:SECURITY_ERR:DOM异常18
答案 0 :(得分:17)
文档域应该是小写的,规则是这样的:
// 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 :(得分:0)
document.domain
应该适用于iframe:
iframe=document.querySelector('iframe');
console.log(iframe.contentDocument.domain)
如果您尝试访问与父框架不同的域中的iframe文档,您将收到您所看到的安全错误。
请注意,子域也被视为不同的域,因此您将遇到跨域问题:A question about cross-domain (subdomain) ajax request