Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript(包括HTTP)中获取完整的主机名?_Javascript_Url_Hostname - Fatal编程技术网

如何在Javascript(包括HTTP)中获取完整的主机名?

如何在Javascript(包括HTTP)中获取完整的主机名?,javascript,url,hostname,Javascript,Url,Hostname,我在获取Javascript中的主机名时遇到问题,我使用了以下代码: document.location.replace("http://"+ document.location.host + "/another-page") 代码工作正常,它获取主机名并用新的URL替换文档URL 问题在于document.location.host,它只返回如下主机:www.xxx.com,而不返回http部分。这不是一种可伸缩的方式,因为站点在本地运行在http上,在服务器上运行在https上。我不想手动

我在获取Javascript中的主机名时遇到问题,我使用了以下代码:

document.location.replace("http://"+ document.location.host + "/another-page")
代码工作正常,它获取主机名并用新的URL替换文档URL

问题在于
document.location.host
,它只返回如下主机:www.xxx.com,而不返回http部分。这不是一种可伸缩的方式,因为站点在本地运行在http上,在服务器上运行在https上。我不想手动更改http部分


那么,是否有更好的解决方案可以自动获取包括http在内的主机全名

这是您要找的
位置.原点

或者您只能使用此
位置获取协议。协议

更多信息请参见此处-

console.log('location.protocol-',location.protocol-);

console.log('location.origin-',location.origin)这是您要找的
位置.原点

或者您只能使用此
位置获取协议。协议

更多信息请参见此处-

console.log('location.protocol-',location.protocol-);
console.log('location.origin-',location.origin)