javascript 文档 cookie / 出现错误“非法的 document.domain 值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9125186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document cookie / Got an error "Illegal document.domain value"
提问by user1186966
while using document cookie
使用文档 cookie 时
<script typ="text/javascript">
document.domain="example.com";
</script>
I got an error.i don't know the exact problem.
我有一个错误。我不知道确切的问题。
Error: uncaught exception: [Exception... "Illegal document.domain value" code: "1009" nsresult: "0x805303f1 (NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN)" location: ""]
错误:未捕获的异常:[异常...“非法文档.域值”代码:“1009”nsresult:“0x805303f1(NS_ERROR_DOM_BAD_DOCUMENT_DOMAIN)”位置:“”]
回答by Ates Goral
According to the HTML spec, document.domain
is read-only. However, Firefox allows setting it to a superdomain:
根据 HTML 规范,document.domain
是只读的。但是,Firefox 允许将其设置为超级域:
https://developer.mozilla.org/en/document.domain
https://developer.mozilla.org/en/document.domain
In the DOM HTML specification, this property is listed as being read-only. However, Mozilla will let you set it to a superdomain of the current value, constrained by its base domain. For example, on developer.mozilla.org it is possible to set it to "mozilla.org" but not "mozilla.com" or "org".
在 DOM HTML 规范中,此属性被列为只读。但是,Mozilla 会让您将其设置为当前值的超级域,受其基本域的约束。例如,在 developer.mozilla.org 上,可以将其设置为“mozilla.org”而不是“mozilla.com”或“org”。
The error you're getting clearly states that you're making an illegal domain assignment.
您收到的错误清楚地表明您进行了非法域分配。
You mention cookies in your question. If you're trying to set the domain part of a cookie, setting document.domain
has nothing to do with it. You need to provide a domain parameter as part of the cookie string that you're assigning:
您在问题中提到了 cookie。如果您尝试设置 cookie 的域部分,则设置document.domain
与此无关。您需要提供域参数作为您分配的 cookie 字符串的一部分:
document.cookie = "foo=bar;domain=example.com";
More info here: https://developer.mozilla.org/en/DOM/document.cookie
更多信息在这里:https: //developer.mozilla.org/en/DOM/document.cookie
回答by lwpro2
it works on IE as well. it's for same origin, we need to shorten the domain name.
它也适用于 IE。这是同源的,我们需要缩短域名。