location.host与location.hostname和跨浏览器的兼容性?

本文翻译自:location.host vs location.hostname and cross-browser compatibility?

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). 如果他们使用某种Web代理访问域名(因为它倾向于打破js),我们希望显示一个基于小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: 在Firefox 5和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. W3Schools说主机包含端口。

Should location.host/hostname be validated or can we be pretty certain in IE6+ and all the others it will exist? 应该验证location.host/hostname还是我们可以在IE6 +中确定其他所有其他内容?


#1楼

参考:https://stackoom.com/question/SDi6/location-host与location-hostname和跨浏览器的兼容性


#2楼

互动链接解剖学

As a little memo: the interactive link anatomy 作为一个小备忘录: 交互式链接解剖

-- -

In short (assuming a location of http://example.org:8888/foo/bar#bang ): 简而言之(假设http://example.org:8888/foo/bar#bang的位置):

  • hostname gives you example.org hostname为您提供example.org
  • host gives you example.org:8888 host给你example.org:8888

#3楼

Your primary question has been answered above. 您的主要问题已在上面得到解答。 I just wanted to point out that the regex you're using has a bug. 我只是想指出你正在使用的正则表达式有一个错误。 It will also succeed on foo-domain.com which is not a subdomain of domain.com 它也将在foo-domain.com上成功,而foo-domain.com不是domain.com的子domain.com

What you really want is this: 你真正想要的是这个:

/(^|\.)domain\.com$/

#4楼

If you are insisting to use the window.location.origin You can put this in top of your code before reading the origin 如果您坚持使用window.location.origin您可以在阅读origin之前将其置于代码之上

if (!window.location.origin) {
  window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}

Solution

PS: For the record, it was actually the original question. PS:为了记录,它实际上是原始问题。 It was already edited :) 它已经编辑:)


#5楼

Just to add a note that Google Chrome browser has origin attribute for the location. 只是添加一条说明Google Chrome浏览器具有该位置的原始属性。 which gives you the entire domain from protocol to the port number as shown in the below screenshot. 它为您提供从协议到端口号的整个域,如下面的屏幕截图所示。 chrome开发工具


#6楼

host just includes the port number if there is one specified. 如果有指定的端口号,则主机只包含端口号。 If there is no port number specifically in the URL, then it returns the same as hostname. 如果URL中没有专门的端口号,则返回与hostname相同的端口号。 You pick whether you care to match the port number or not. 您选择是否要匹配端口号。 See https://developer.mozilla.org/en/window.location for more info. 有关详细信息,请参阅https://developer.mozilla.org/en/window.location

I would assume you want hostname to just get the site name. 我想你想要主机名来获取网站名称。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值