文档域错误

document.domain bug

本文关键字:错误 文档      更新时间:2023-09-26
Permission denied for <http://www.guy.lt> (document.domain=<http://www.guy.lt>) to get property Window.document from <http://www.guy.lt> (document.domain has not been set).

如果这不是错误,那么如何解释这种行为?(或至少是错误消息)当然,如何解决它?

另一个奇怪的事情是:

debug.log('0');
document.domain = 'guy.lt';
debug.log('1');
document.domain = 'wwww.guy.lt';
debug.log('2');

debug.log('2')永远不会触发。但是,console中没有错误。脚本只是停止执行。

我无法根据您的问题推断您做错了什么,但要启用跨域脚本,您必须document.domain设置为域的相同公共部分。还相关:

  • 如果一侧使用foo.guy.lt而另一侧使用bar.guy.lt则必须在两侧设置document.domain = "guy.ly"

  • 如果将document.domain设置为 guy.lt则实际域必须guy.lt本身或 guy.lt 的子域。您不能编造(子)域名。

  • 你总是必须显式指定document.domain的值,即使该值是实际的域本身。

  • 您永远无法将document.domain更改回更具体的子域。因此,如果实际域是 www.guy.lt ,则可以将document.domain更改为 guy.lt 。但是,在此更改之后,您无法将其更改回 www.guy.lt .

例子:

// 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设置为相同的值时,才允许通过document.domain进行跨网域访问。 这是一项必要的安全措施;否则something.company.com可以将document.domain设置为company.com并从company.com中读取内容。 实际上,只有当company.com通过将document.domain设置为 company.com 来明确选择加入时,它才能做到这一点。