location 参数详解 实例展示

  • location.href 设置或返回完整的 URL。在浏览器的地址栏上怎么显示它就怎么返回。
  • location.protocol:协议名.取值有'https:','https:','file:' 等等。
  • location.host=location.hostname+location.port:主机名+端口号
  • location.hostname:主机名
  • location.port:端口号
  • location.pathname:路径
  • location.search:查询串.从问号 (?) 开始的 URL(查询部分)
  • location.hash:书签名 设置或返回从井号 (#) 开始的 URL(锚:a标签中设置的href值)。如果地址里没有“#”,则返回空字符串。
  • document.domain:  域名

栗子来了-------假设有这么一个地址:

https://www.baidu.com:8080/js/index.html?id=_1234567&name=my_project#here

location.href           // "https://www.baidu.com:8080/js/index.html?id=_1234567&name=my_project#here"

location.protocol    // "https:"     注意带有冒号":"

location.host          // "www.baidu.com:8080"

location.hostname //  "www.baidu.com"

document.domain // "www.baidu.com" 或者"baidu.com" 因为domain能够被手动修改,所以可能会是以上的值,具体情况可以自行百度

location.port          // "8080"  如果采用默认的80端口(即使添加了:80 如:www.baidu.com:80),那么返回值并不是默认的80而是空字符 ""

location.pathname //   "/js/index.html" 注意js前面的斜杠"/"要带上

location.search      //   "?id=_1234567&name=my_project" "?"号也会带上

location.hash        //    "#here"  如果地址里没有“#”,则返回空字符串。

 

你可能感兴趣的:(学习总结)