以路径为 http://www.baidu.com  为例

console.log("location:"+window.location.href);
>> "https://www.baidu.com/"

console.log("location:"+window.location.hostname);
>> "www.baidu.com/"

console.log("location:"+window.location.protocol);
>> "https:"

console.log("location:"+window.location.host);
>> "www.baidu.com"

console.log("location:"+window.location.port);
>> ""  
#说明一下, 这里是指默认端口, http默认为80端口, https默认为443端口.如果有端口号, 这里会返回端口号的字符串

 

看一下   window.location 具体有哪些内容:

 

版权声明:本文为shuiche原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/shuiche/p/9343174.html