有没有一种非常简单的方法可以从完整的URL开始:
document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah"
提取目标是仅包含主机部分:
aaa.bbb.ccc.com
肯定有一个可靠的JavaScript函数可以做到这一点,但我找不到它。
有没有一种非常简单的方法可以从完整的URL开始:
document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah"
提取目标是仅包含主机部分:
aaa.bbb.ccc.com
肯定有一个可靠的JavaScript函数可以做到这一点,但我找不到它。
http://sub.domain.com/virtualPath/page.htm
的网页。
在页面代码中使用以下内容可以实现以下结果:
属性 | 结果 |
---|---|
window.location.host |
sub.domain.com:8080 或 sub.domain.com:80 |
window.location.hostname |
sub.domain.com |
window.location.protocol |
http: |
window.location.port |
8080 或 80 |
window.location.pathname |
/virtualPath |
window.location.origin |
http://sub.domain.com (可能会包括 :port *****) |
更新:关于 .origin
***** 根据参考文献,浏览器对window.location.origin
的兼容性不明确。我在 Chrome 中进行了检查,如果端口不是 80,则会返回http://sub.domain.com:port
,如果端口是 80,则返回http://sub.domain.com
。
特别感谢 @torazaburo 提醒我。
var root = location.protocol + '//' + location.host;
假设有一个URL地址,例如'http://stackoverflow.com/questions'
,它将返回'http://stackoverflow.com'
hostname
和host
之间的区别在这里展示;host
可能包括**port
**,而hostname
不包括port
。我认为Christian在这里使用了location.host
,所以var root
的值将包括port
值(如果有的话)。正如@Sérgio所说,为什么var root = location.protocol + '//' + location.host
等同于 location.origin
; - Nate Anderson接受的答案对我没有用,因为我想能够处理任意的URL而不仅仅是当前页面的URL。
看一下URL
对象:
var url = new URL("http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah");
url.protocol; // "http:"
url.hostname; // "aaa.bbb.ccc.com"
url.pathname; // "/asdf/asdf/sadf.aspx"
url.search; // "?blah"
使用 document.location
对象及其 host
或 hostname
属性。
alert(document.location.hostname); // alerts "stackoverflow.com"
有两种方法。第一种方法是另一个答案的变体,但此方法考虑了非默认端口:
function getRootUrl() {
var defaultPorts = {"http:":80,"https:":443};
return window.location.protocol + "//" + window.location.hostname
+ (((window.location.port)
&& (window.location.port != defaultPorts[window.location.protocol]))
? (":"+window.location.port) : "");
}
但我更喜欢这种更简单的方法(适用于任何URI字符串):
function getRootUrl(url) {
return url.toString().replace(/^(.*\/\/[^\/?#]*).*$/,"$1");
}
假设您有这个URL路径:
http://localhost:4200/landing?query=1#2
因此,您可以通过以下位置值为自己提供服务:
window.location.hash: "#2"
window.location.host: "localhost:4200"
window.location.hostname: "localhost"
window.location.href: "http://localhost:4200/landing?query=1#2"
window.location.origin: "http://localhost:4200"
window.location.pathname: "/landing"
window.location.port: "4200"
window.location.protocol: "http:"
window.location.search: "?query=1"
现在我们可以得出结论,您正在寻找:
window.location.hostname
尝试
document.location.host
或者
document.location.hostname
window.location.origin
我知道有点晚,但是我写了一个简洁的函数,使用了一些ES6语法
function getHost(href){
return Object.assign(document.createElement('a'), { href }).host;
}
它也可以用ES5编写,例如:
function getHost(href){
return Object.assign(document.createElement('a'), { href: href }).host;
}
Object.assign
,但在我的工作范围内,这并不重要。请检查以下内容:
alert(window.location.hostname);
www.domain.com
还有:window.location.host
将返回带有端口的域名,例如www.example.com:80
完整参考请查看Mozilla开发者网站。
window.location.origin
在IE9中未定义(仅适用于IE11以上版本)。参考这个答案可解决该问题。 - Victor Zakharovwindow.location.pathname
will NOT give/virtualPath
. It will give/virtualPath/page.htm
- ebyt