如何从当前页面使用JavaScript获取主机URL [英] How to get the host URL using JavaScript from the current page
本文介绍了如何从当前页面使用JavaScript获取主机URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于我在以下页面上:
http://www.webmail.com/pages/home.aspx
如何使用JavaScript检索主机名("http://www.webmail.com"
)?
How can I retrieve the host name ("http://www.webmail.com"
) with JavaScript?
推荐答案
// will return the host name and port
var host = window.location.host;
或可能
var host = window.location.protocol + "//" + window.location.host;
或者如果您喜欢串联
var protocol = location.protocol;
var slashes = protocol.concat("//");
var host = slashes.concat(window.location.host);
// or as you probably should do
var host = location.protocol.concat("//").concat(window.location.host);
// the above is the same as origin, e.g. "https://stackoverflow.com"
var host = window.location.origin;
如果您有或希望有自定义端口,请使用 window.location.host
而不是 window.location.hostname
>
If you have or expect custom ports use window.location.host
instead of window.location.hostname
这篇关于如何从当前页面使用JavaScript获取主机URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文