浏览器的三种弹窗
-
alert() 是在浏览器弹出一个提示框
// window.alert("我是一个弹出框")
-
confirm() 是浏览器弹出一个询问框
// var result = window.confirm("你确定要关闭浏览器吗?")
// console.log(result)
-
prompt 是在浏览器弹出一个输入框
// var str = window.prompt('请输入内容') // console.log(str)
获取浏览器窗口大小
// var windowHeight = window.innerHeight;
// console.log(windowHeight)
// var windowWidth = window.innerWidth;
// console.log(windowWidth)
// 一般情况下,window可以省略
浏览器location对象
- location对象属性
location对象属性 | 返回值 |
---|---|
location.href | 获取或者设置整个URL |
location.host | 返回主机(域名)www.baidu.com |
location.port | 返回端口号,如果未写返回空字符串 |
location.pathname | 返回路径 |
location.search | 返回参数 |
location.hash | 返回片段 #后面内容常见于链接 锚点 |
重点记住: href
和search
-
location对象方法
location对象方法 返回值 location.assign() 跟href一样,可以跳转页面(也称为重定向页面) location.replace() 替换当前页面,因为不记录历史,所以不能后退页面 location.reload() 重新加载页面,相当于刷新按钮或者 f5 ,如果参数为true 强制刷新 ctrl+f5
浏览器history对象
history对象方法 | 作用 |
---|---|
back() | 可以后退功能 |
forward() | 前进功能 |
go(参数) | 前进后退功能,参数如果是 1 前进1个页面 如果是 -1 后退1个页面 |
浏览器的navigator对象
navigator对象属性 | 作用 |
---|---|
navigator.userAgent | 是获取的浏览器的整体信息 |
navigator.appName | 是获取浏览器的名称 |
navigator.appVersion | 是获取浏览器的版本号 |
navigator.platform | 是获取的当前计算机的操作系统 |
浏览器对象常见的事件
-
窗口加载事件(onload)
window.onload = function(){ alert('box') }
-
调整窗口大小事件(onresize)
window.onresize = function() {}
-
滚动事件(onscroll)
window.onscroll = function(){ alert('我滚动了') }
- 注意点
// scrollTop // 获取的是页面上滚动的距离 // 一共有三个获取方式 // document.body.scrollTop // document.documentElement.scrollTop // window.pageYOffset // 区别: // Chrome和FireFox浏览器 // 没有DOCTYPE声明的时候,用document.body.scrollTop // 有DOCTYPE声明的时候,用document.documentElement.scrollTop // Safari // 使用window.pageYOffset // IE浏览器 // 没有DOCTYPE声明的时候,用这两个都可以 // 有DOCTYPE声明的时候,只能用document.documentElement.scrollTop // scrollLeft // 获取页面向左滚动的距离 // 也是三个方法: // document.body.scrollLeft // document.documentElement.scrollLeft // window.pageXOffset // 三个之间的区别和之前的scrollTop一样
# 定时器
-
setTimeout()和其关闭
//var time1= setTimeout(function(){ // console.log("时间到了") // },5000) // clearTimeout(time1); // 关闭以后,定时器里面的函数就不会执行了
-
setInterval()和其关闭
//var time2= setInterval(function(){ // console.log(Math.random()) // },2000); // clearInterval(time2);// 关闭以后,定时器里面的函数就不会执行了