document.domain 的 Chrome 解决方案

发布于 2024-11-30 07:40:23 字数 131 浏览 1 评论 0原文

由于某种原因,chrome 不再支持 document.domain,并且在包含子域的 iframe 和包含 iframe 的子域中读取该行时会发出错误。这附近还有吗?

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

For some reason chrome doesn't support document.domain any more and spits out an error when the line is read in the iframe containing a subdomain and the subdomain containing the iframe. Is there anyway around this?

Error: Uncaught Error: SECURITY_ERR: DOM Exception 18

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2024-12-07 07:40:23

文档域应小写,规则如下:

// 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. 

Document domain should be lowercase and the rules are like this:

// 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. 
じее 2024-12-07 07:40:23

只要您位于同一域中,document.domain 就应该适用于 iframe:

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

如果您尝试访问与父框架位于不同域的 iframe 文档,您将获取您所看到的安全错误。

请注意,子域也被视为不同的域,因此您将遇到跨域问题: 一个关于跨域(子域)ajax请求的问题

document.domain should work for iframes as long as you're on the same domain:

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

If you're trying to access the document of an iframe that's on a different domain than the parent frame, you'll get the security error you're seeing.

Note that subdomains are considered different domains too, so you'll run into cross domain issues: A question about cross-domain (subdomain) ajax request

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文