全局对象
1 | window 当前窗口,如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外的 window 对象。 |
窗口位置
1 | screen.width 浏览器屏幕宽度 |
注:以下对象都为全局对象,即 window.history === history
history
forward(前进)
等价于 history.go(1)
1 | history.forward() |
back(后退)
等价于 history.go(-1)
1 | history.back() |
go(跳转至指定历史页面)
指定值大于当前历史长度时无效
1 | history.go(2) |
pushState(无刷新的插入一条历史记录)
并不会刷新页面 参数:stateObject 状态对象、title 标题、url 历史记录地址 适用于单页应用,在完成一次Ajax请求的同时,为浏览器创造一个历史状态。
1 | // 假如当前URL为:http://example.com/getFinaData.php |
replaceState(替换当前历史状态)
并不会刷新页面 参数:stateObject 状态对象、title 标题、url 历史记录地址
1 | history.replaceState({year: 2018}, '页面标题', 'getFinaData.php?year=2018') |
location
下面的接口不止用于获取当前URL信息,同样可用于设置URL信息。
protocol(协议)
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’ |
host(主机域名加端口号)
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’ |
hostname(主机域名)
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’location.hostname |
port(端口号)
默认端口号是 80
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’ |
pathname(路径名)
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’ |
search(查询参数)
?
号之后的部分,不包含 hash
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’ |
hash(锚点)
1 | // url :'https://www.google.com/search?q=may&oq=may#hash’ |
href(完整路径)
1 | // url :'https://www.google.com/search?q=may&oq=may#hash' |
assign(加载新页面)
会增加一个历史状态,不会替换掉当前历史
1 | location.assign('https://www.baidu.com') |
replace(替换页面)
直接替换当前历史状态
1 | location.replace('https://www.baidu.com') |
reload(刷新页面)
参数:forced 是否强制从服务器加载资源,如果为false则从浏览器缓存中读取网页资源。默认:false。
1 | location.reload() |
naviagtor
platform(当前浏览器所在的操作系统)
1 | navigator.platform // 'MacIntel' |
userAgent(当前浏览器版本信息)
1 | navigator.userAgent // 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' |
获取客户端操作系统
1 | // 返回值:Windows、Unix、Linux、Mac、Iphone、Android、Unknown |
获取客户端浏览器信息
1 | function getBrowser() { |