window.location各个属性-笔记

Location 对象包含有关当前 URL 的信息。

Location 对象是 window 对象的一部分,可通过 window.Location 属性对其进行访问。

直接看例子吧

http://test-hao.ping.com/wap/huo/yiduanshi.html?version=1.0&name=123#number=1

如果直接

console.log(window.location);

结果是这样子的:

protocol: "http:"                             //返回URL协议
host: "test-hao.ping.com"                     //返回URL的主机名和端口
hostname: "test-hao.ping.com"           //返回URL的主机名
port: ""                                      //返回URL服务器使用的端口号
pathname: "/wap/huo/yiduanshi.html"           //返回URL路径名
search: "?version=1.0&name=123"               //返回URL的查询部分
hash: "#number=1"                             //返回URL的锚部分
href: "http://test-hao.ping.com/wap/huo/yiduanshi.html?version=1.0&name=123#number=1"  //返回完整的URL

解释:

1:http协议端口号80为默认端口号,如果端口号为80,则返回空。

2:hash返回的部分为#加上后边的内容。

posted @ 2020-04-21 15:28  苹果π  阅读(229)  评论(0编辑  收藏  举报