晓峰 Rotating Header Image

host&domain

FROM:https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript

The same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin. This policy dates all the way back to Netscape Navigator 2.0.
不同域禁止相互访问document对象

Mozilla considers two pages to have the same origin if the protocol, port (if one is specified), and host are the same for both pages.
同域满足的条件是1.相同协议(如http和https为不同协议)2.端口(如:80和:8080为不同端口号)3.相同主机(如ulhoo.com和www.ulhoo.com为不同主机名)

There is one exception to the same origin rule. A script can set the value of document.domain to a suffix of the current domain. If it does so, the shorter domain is used for subsequent origin checks.
子域可以设置为更高一级的域,反过来做是禁止的,例如”http://www.ulhoo.com”的document.domain对象可以设置为”ulhoo.com”,而”http://ulhoo.com”的document.domain对象不可以设置为”www.ulhoo.com”

—————————————————————–
举个例子:

步骤1.在www.ulhoo.com域下,open一个ulhoo.com域的窗口

var win1 = window.open("http://ulhoo.com/blog"); 

步骤2.同时设置ulhoo.com窗口的domain为”ulhoo.com”

document.domain = "ulhoo.com";

ps:我们是不能在www.ulhoo.com下设置win1窗口的domain属性,必须依赖”http://ulhoo.com/blog”的自身脚本去设置,此时即使打开”http://www.ulhoo.com/blog”也必须必须设置document.domain为相同值。

One Comment

Leave a Reply