Javascript location.host 与 location.hostname 和跨浏览器兼容性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6725890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
location.host vs location.hostname and cross-browser compatibility?
提问by anonymous-one
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?
那是因为端口实际上不在地址栏中吗?
W3Schoolssays 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+ 和所有其他它将存在?
回答by abernier
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 youexample.org
host
gives youexample.org:8888
hostname
给你example.org
host
给你example.org:8888
回答by jfriend00
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. You pick whether you care to match the port number or not. See https://developer.mozilla.org/en/window.locationfor more info.
如果指定了端口号,则主机只包含端口号。如果 URL 中没有专门的端口号,则返回与主机名相同的端口号。您选择是否关心匹配端口号。有关更多信息,请参阅https://developer.mozilla.org/en/window.location。
I would assume you want hostname to just get the site name.
我假设您希望主机名仅获取站点名称。
回答by Kenneth Palaganas
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: '');
}
PS: For the record, it was actually the original question. It was already edited :)
PS:为了记录,这实际上是原始问题。它已经被编辑了:)
回答by bluesmoon
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
不是的子域上取得成功domain.com
What you really want is this:
你真正想要的是这个:
/(^|\.)domain\.com$/
回答by Igor Dymov
MDN: https://developer.mozilla.org/en/DOM/window.location
MDN:https: //developer.mozilla.org/en/DOM/window.location
It seems that you will get the same result for both, but hostname
contains clear host name without brackets or port number.
似乎您将获得相同的结果,但hostname
包含不带括号或端口号的明确主机名。