如何从当前页面使用 javascript 获取主机 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6042007/
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
how to get the host url using javascript from the current page
提问by karthik k
Given that I'm on the following page:
鉴于我在以下页面上:
http://www.webmail.com/pages/home.aspx
How can I retrieve the host name ("http://www.webmail.com"
) with JavaScript?
如何"http://www.webmail.com"
使用 JavaScript检索主机名 ( )?
回答by Eric Herlitz
var host = window.location.hostname;
or possibly
或者可能
var host = "http://"+window.location.hostname;
or if you like concatenation
或者如果你喜欢串联
var protocol = location.protocol;
var slashes = protocol.concat("//");
var host = slashes.concat(window.location.hostname);
回答by Quentin
To get the hostname: location.hostname
获取主机名: location.hostname
But your example is looking for the scheme as well, so location.origin
appears to do what you want in Chrome, but gets not mention in the Mozdev docs. You can construct it with
但是您的示例也在寻找该方案,因此location.origin
似乎可以在 Chrome 中执行您想要的操作,但在 Mozdev 文档中没有提及。你可以用
location.protocol + '//' + location.hostname
If you want the port number as well (for when it isn't 80) then:
如果您还需要端口号(当它不是 80 时),则:
location.protocol + '//' + location.host
回答by Monso
You can get the protocol, host, and port using this:
您可以使用以下命令获取协议、主机和端口:
window.location.origin
Browser compatibility
浏览器兼容性
Desktop
桌面
| Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|----------------------------------|-------|-----------------|-------------------|-------|--------------------------------------------|
| (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| 30.0.1599.101 (possibly earlier) | ? | 21.0 (21.0) | 11 | ? | 7 (possibly earlier, see webkit bug 46558) |
Mobile
移动的
| Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|----------------------------------|-------|------------------------|----------|--------------|--------------------------------------------|
| (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| 30.0.1599.101 (possibly earlier) | ? | 21.0 (21.0) | ? | ? | 7 (possibly earlier, see webkit bug 46558) |
All browser compatibility is from Mozilla Developer Network
所有浏览器兼容性均来自Mozilla Developer Network
回答by user3601578
let path = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
回答by GordyD
This should work:
这应该有效:
window.location.hostname
回答by Mahmoud salah eldien saber
Depending on your needs, you can use one of the window.location
properties.
In your question you are asking about the host, which may be retrieved using window.location.hostname
(e.g. www.example.com
). In your example you are showing something what is called origin, which may be retrieved using window.location.origin
(e.g. http://www.example.com
).
根据您的需要,您可以使用其中一个window.location
属性。在您的问题中,您询问的是host,可以使用window.location.hostname
(例如www.example.com
)检索它。在你的例子中,你展示了一些叫做origin 的东西,它可以使用window.location.origin
(例如http://www.example.com
)检索。
var path = window.location.origin + "/";
//result = "http://localhost:60470/"
回答by Gosso
I like this one depending of purpose
我喜欢这个取决于目的
window.location.href.split("/")[2] == "localhost:17000" //always domain + port
You can apply it on any url-string
您可以将其应用于任何 url-string
var url = "http://localhost:17000/sub1/sub2/mypage.html?q=12";
url.split("/")[2] == "localhost:17000"
url.split("/")[url.split("/").length-1] == "mypage.html?q=12"
Removing protocol, domain & path from url-string (relative path)
从 url-string(相对路径)中删除协议、域和路径
var arr = url.split("/");
if (arr.length>3)
"/" + arr.splice(3, arr.length).join("/") == "/sub1/sub2/mypage.html?q=12"