js location 与 navigator对象

location对象

js location 与 navigator对象

href: 地址栏

protocol: 协议

host:  主机名端口号 或 域名  hostname: 主机名  port: 端口号

pathname: 路径, host后、hash前或search前的路径

hash: #号及后面的内容,如遇到?则停止,后续是search

search: ?及其后面的内容, 如遇到#则停止,后续是hash

常用方法

 

  • 设置页面跳转

location.href = "http://www.jd.com";  // 有历史记录

location.replace("http://www.jd.com");  // 没历史记录

  • 页面重载

location.reload();

  • 获取search中的参数值

 

navigator对象

js location 与 navigator对象

platform:  用户浏览器运行的系统

userAgent:  用户浏览器类型

  • 检测是否为IE浏览器

const isIE = /Trident/.test(navigator.userAgent) || /MSIE/.test(navigator.userAgent)

// true则是IE