1.window.onload=函数 页面所有内容都加载完毕后触发
window.onunload=函数 页面关闭时触发
var box = document.getElementById('box') window.onload=function(){ console.log(1) }
2 window.location 操作浏览器地址栏地址(url)
console.log(location)
3.location对象的属性 url 同一资源定位符
url组成: 协议://主机名(域名):端口号/路径?查询字符串#锚点
href:地址栏中相对完整的url字符串
search:url中查询字符串的部分
host:主机名+端口号
hostname:端口号
hash:锚点
4.location方法
location.assign('url字符串')用于页面跳转,可以记录页面跳转的历史
var box = document.getElementById('box') var bt = document.getElementById('bt') bt.onclick=function(){ location.assign('http://www.baidu.com') }
location.replace('url字符串')用于页面跳转,不记录历史
var box = document.getElementById('box') var bt = document.getElementById('bt') bt.onclick=function(){ location.replace('http://www.baidu.com') }
location.reload()刷新页面 传一个true 强制刷新页面
bt.onclick=function(){
location.reload()
}
5.location.href= ' url字符串 ' 给href属性赋值 跟assign类似
6.history对象
(1)history.back()返回上一个页面
(2)history.forward()前进到下一个页面
(3)go(步数) 正数代表前进,负数代表后退
bt1.onclick=function(){ history.go(1) //前进一步 } bt2.onclick=function(){ history.go(-1) //后退一步 }
7. navigator.userAgent 返回识别浏览器客户端和浏览器的字符串
bt.onclick=function(){
console.log( navigator.userAgent)
}
8.定时器
(1)setInterval(回调函数,间隔时间) 每隔一段时间就调用一次
setInterval(function(){ console.log(1) },1000)
(2)setTimeout(回调函数,间隔时间) 时间到了后只执行一次
setTimeout(function(){ console.log(5) },3000)
9.清除定时器
(1)setInterval 对应的是 clearInterval ()
var time1=setInterval(function(){ console.log(1) },1000) bt.onclick=function(){ clearInterval(time1) }
(2)setTimeout 对应的是clearTimeout()
var time2= setTimeout(function(){ console.log(2) },3000) bt.onclick=function(){ clearTimeout(time2) }