location.host vs location.hostname和跨浏览器兼容性? [英] location.host vs location.hostname and cross-browser compatibility?

查看:376
本文介绍了location.host vs location.hostname和跨浏览器兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中一个是最有效的vs检查用户代理是否通过正确的域进行访问。



我们想显示一个基于js的小顶端bar'风格的警告,如果他们使用某种web代理访问域(因为它往往会打破js)。



我们正在考虑使用以下内容:

  var r = /.*domain\.com$/; 
if(r.test(location.hostname)){
// showMessage ...
}

这将照顾我们曾经使用的任何子域名。



我们应该使用主机名还是主机名?



在Firefox 5和Chrome 12中:

  console.log(location.host); 
console.log(location.hostname);

..对于两者都是相同的。



是否因为端口实际上不在地址栏中?





作为一个小备忘录:交互式链接剖析



-




总之(假设位置 http:// example.org:8888/foo/bar#bang ):


  • hostname example.org

  • host 给你例子。要么g:8888


Which one of these is the most effective vs checking if the user agent is accessing via the correct domain.

We would like to show a small js based 'top bar' style warning if they are accessing the domain using some sort of web proxy (as it tends to break the js).

We were thinking about using the following:

var r = /.*domain\.com$/;
if (r.test(location.hostname)) {
    // showMessage ...
}

That would take care of any subdomains we ever use.

Which should we use host or hostname?

In Firefox 5 and Chrome 12:

console.log(location.host);
console.log(location.hostname);

.. shows the same for both.

Is that because the port isn't actually in the address bar?

W3Schools says host contains the port.

Should location.host/hostname be validated or can we be pretty certain in IE6+ and all the others it will exist?

解决方案

As a little memo: the interactive link anatomy

--

In short (assuming a location of http://example.org:8888/foo/bar#bang):

  • hostname gives you example.org
  • host gives you example.org:8888

这篇关于location.host vs location.hostname和跨浏览器兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆