有没有一种非常简单的方法可以从完整的 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
*****更新:关于 .origin
***** 正如参考文献所述,浏览器兼容性window.location.origin
尚不清楚。我已经在 chrome 中检查了它,http://sub.domain.com:port
如果端口不是 80,它会返回,http://sub.domain.com
如果端口是 80。
特别感谢@torazaburo 向我提及这一点。
您可以连接位置协议和主机:
var root = location.protocol + '//' + location.host;
对于一个 url,假设'http://stackoverflow.com/questions'
它会返回'http://stackoverflow.com'
接受的答案对我不起作用,因为我希望能够使用任何任意 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");
}
尝试
document.location.host
或者
document.location.hostname
检查这个:
alert(window.location.hostname);
这将返回主机名www.domain.com
和:
window.location.host
将返回带有端口的域名www.example.com:80
如需完整参考,请查看Mozilla 开发者网站。
我使用了另一个 hack,但从未在任何 StackOverflow 响应中看到:使用图像的“src”属性将产生您网站的完整基本路径。例如 :
var dummy = new Image;
dummy.src = '$'; // using '' will fail on some browsers
var root = dummy.src.slice(0,-1); // remove trailing '$'
在类似的 URL 上http://domain.com/somesite/index.html
,
root
将设置为http://domain.com/somesite/
. 这也适用于 localhost 或任何有效的基本 URL。
请注意,这将导致$
虚拟图像上的 HTTP 请求失败。您可以使用现有图像来避免这种情况,只需稍作更改代码即可。
另一种变体使用虚拟链接,对 HTTP 请求没有副作用:
var dummy = document.createElement ('a');
dummy.href = '';
var root = dummy.href;
不过,我并没有在每个浏览器上都对其进行测试。
假设你有这个 url 路径:
http://localhost:4200/landing?query=1#2
因此,您可以通过location values为自己服务,如下所示:
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
我想指定一些东西。如果有人想用我需要的路径获取整个 url,可以使用:
var fullUrl = window.location.protocol + "//" + window.location.hostname + window.location.pathname;
我知道这有点晚了,但我用一点 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;
}
当然 IE 不支持Object.assign
,但在我的工作中,这并不重要。
正则表达式提供了更多的灵活性。
//document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah
//1.
var r = new RegExp(/http:\/\/[^/]+/);
var match = r.exec(document.location.href) //gives http://aaa.bbb.ccc.com
//2.
var r = new RegExp(/http:\/\/[^/]+\/[^/]+/);
var match = r.exec(document.location.href) //gives http://aaa.bbb.ccc.com/asdf
我的解决方案适用于包括 Microsoft Internet Explorer 在内的所有 Web 浏览器,并且不使用任何正则表达式,它的灵感来自 Noah Cardoza 和 Martin Konecny 解决方案:
function getHostname(href) {
if (typeof URL === 'object') {
// workaround for MS IE 11 (Noah Cardoza's solution but without using Object.assign())
var dummyNode = document.createElement('a');
dummyNode.href = href;
return dummyNode.hostname;
} else {
// Martin Konecny's solution
return new URL(href).hostname;
}
}
您可以使用拆分 URL 字符串/
const exampleURL = "Https://exampleurl.com/page1/etc/etc"
const URLsplit = exampleURL.split("/")
console.log(URLsplit)
console.log(URLsplit[2])
结果。exampleurl.com