document.domain = document.domain 的作用是什么?
Orbited(Comet 服务器)的客户端 JS 组件要求,如果服务器运行在不同的域或移植到 JS 本身,您必须
document.domain = document.domain;
在加载任何其他 JS 之前执行。 (请参阅文档。)
这是做什么的?它看起来像一个NOOP! (我查了一下,确实有必要。)
The client-side JS component of Orbited (a Comet server), requires that if the server is running on a different domain or port to the JS itself, you must execute
document.domain = document.domain;
before any other JS is loaded. (See the documentation.)
What does this do? It looks like a NOOP! (I've checked and it is in fact necessary.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我实际上写了这段代码。
当尝试跨子域/端口 comet 时,iframe 需要与父框架具有相同的
document.domain
值。不幸的是,浏览器在内部存储原始document.domain
值的域名和端口。但 JavaScript 中的 getter 和 setter 对端口一无所知。所以问题是这样的:如果顶部框架document.domain
是('example.com', 80)
,底部框架是('comet. example.com', 80)
,如何让底部框架也成为('example.com', 80)
?您不能,因为更改主机名部分必然会导致端口设置为
null
,因此您能做的最好的事情就是('example.com', null)
在底部框架中。因此,顶部框架也需要设置为该值,并且设置document.domain=document.domain
就可以做到这一点。它将浏览器中的内部表示从('example.com', 80)
更改为('example.com', null)
,然后所有内容都匹配并交叉端口/子域帧通信正常。I actually wrote this code.
When trying to do cross-subdomain/port comet, the iframe needs to have the same
document.domain
value as the parent frame. Unfortunately, the browser stores the domain name AND port internally for the originaldocument.domain
value. But the getter and setter in javascript knows nothing about the port. So the problem is this: if the top framedocument.domain
is('example.com', 80)
, and the bottom frame is('comet.example.com', 80)
, how do you get the bottom frame to be('example.com', 80)
as well?You can't, as changing the hostname portion will necessarily cause the port to be set to
null
, so the best you can do is('example.com', null)
in the bottom frame. So the top frame also needs to be set to that value, and settingdocument.domain=document.domain
does just that. It changes the internal representation in the browser from('example.com', 80)
to('example.com', null)
and then everything matches up and cross-port/subdomain frame communication works.浏览器区分
(a) 未明确设置时的 document.domain
和
(b) 明确设置时的 document.domain
...即使它们返回相同的值。
显式设置该值表示与另一个子域(在同一父域下)上的脚本“合作”的意图。
如果父页面和外部脚本都显式地将 document.domain 设置为相同的值,则可以绕过同源策略限制,并且每个脚本可以访问彼此上下文的所有(否则受限制的)对象和属性。
Browsers distinguish between
(a) document.domain when not explicitly set
and
(b) document.domain when explicitly set
... even if they return the same value.
Explicitly setting the value indicates intent to "cooperate" with a script on another subdomain (under the same parent domain).
If BOTH the parent page AND the external script explicitly set document.domain to the same value, the same-origin policy restriction may be bypassed and each script may access all the (otherwise restricted) objects and properties of each others' contexts.
我在此网站上找到了以下信息:devguru。更具体地说,这是引用:
在我看来,它允许同一域的跨站点脚本编写(即使子域不同)。
我想如果你不接触 document.domain,js 引擎只允许来自同一域的其他 javascript。借助该属性,您将能够部署到其他子域,例如轨道文档状态。
I found the following info on this site: devguru. More concretely, here's the quote:
It seems to me that it allows cross site scripting for same domain (even if subdomain is different).
I would suppose that if you don't touch document.domain, the js engine only allows other javascripts from same domain. With that property, you'll be able to deploy to other sub-domains like the orbited docs state.
如果未明确设置,
document.domain
将从实际 URL 中提取默认值。浏览器将记录document.domain
是否已作为 URL 的默认值或是否已明确设置。两者必须是同一域的默认值,或者必须将两者显式设置为同一域才能正常工作。如果一个是默认的,一个是显式设置的,如果读取则两者都匹配,则两个页面仍将被禁止相互通信。请参阅:https://developer.mozilla.org/en-US/docs /DOM/document.domain
The
document.domain
pulls a default from the actual URL if not explicitly set. Browsers will record ifdocument.domain
has come as a default from the URL or if it was explicitly set. Both must be a default for the same domain or both must be explicitly set to the same domain for this to work. If one is default and one is explicitly set, both matching if read, the two pages will still be forbidden from talking with each other.See: https://developer.mozilla.org/en-US/docs/DOM/document.domain