js-浏览器的url各种获取

133 阅读1分钟

key:浏览器地址栏 url变化 不刷新 刷新页面;

前言:

  • a标签href跳转:会触发url请求,但是不会触发整个应用页面加载;
  • location.href跳转:相当于刷新页面;
  • history.pushState() 跳转:不会触发整个应用页面重新加载,url会改变地址栏的;

window.location属性:

window.location

返回值

.origin

站点主地址(协议 + 主机名 + 端口)

.protocol

协议架构 (http: 或者 htts:)

.host

域名 + 端口

.port

端口

.pathname

最前页的 '/' 后面跟的路径

.search

? 后跟的查询字符串

.hash

从 # 号开始的部分

.href

完整网址

举例segmentfault.com:8080/search?q=前端小智#2

window.location.origin    → '"https://segmentfault.com'
               .protocol  → 'https:'
               .host      → 'segmentfault.com:8080'
               .hostname  → 'segmentfault.com'
               .port      → '8080'
               .pathname  → '/search'
               .search    → '?q=前端小智'  没有的话是 ""空字符
               .hash'#2'
               .href      → 'https://segmentfault.com/search?q=前端小智#2'