废话不说,代码最直观:

   alert(window.location.hostname);
   alert(document.location.hostname);
   alert(document.domain);

window有一个location对象;document也有一个location对象!几乎是一样的。

下面两个例子,显示location.hostname 和 location.host的区别:

如果访问的页面是 http://my.com/getdomain.html,则window.location的属性值为:

   href=http://my.com/getdomain.html
   hostname=my.com
   host=my.com
   pathname=/getdomain.html
   hash=
   port=
   search=
   protocol=http:

如果访问的页面是 http://127.0.0.1:8080/getdomain.html,则window.location的属性值为:

   href=http://127.0.0.1:8080/getdomain.html
   hostname=127.0.0.1
   host=127.0.0.1:8080
   hash=
   port=8080
   pathname=/getdomain.html
   search=
   protocol=http: