JavaScript BOM对象

window是浏览器中的一个实例,在浏览器中,window具有双从角色,它既是通过javascript访问浏览器窗口的一个接口,又是ECMAScript所规定的全局对象
window全局变量 window.num=1 = var num=1

window对象的方法:

window.alert("")
window.prompt(''提示文字")
window.confirm("提示文字")  //确定 返回true   取消返回false
window.open()  //打开一个新窗口
window.close()  //关闭当前窗口
window.setTimeout()  //定时器 超时调用
window.setInterval()  //定时器 间歇调用
var time1=setTimeout(function(){
	console.log(1)
},2000)
clearTimeout(time1)
var timer=null
timer=setInterval(function(){
console.log(0)
clearInterval(timer)
},1000)

location对象

location.href   //返回当前完整url
location.hash    //返回url  #后面的内容包括#
location.pathname //返回url中的目录或文件名
location.host   //返回服务器名称或端口号
location.search //返回url查询字符串 ?后面的内容包括?
location.hostname //返回不带端口号的服务器名称
location.port  //返回url指定的端口号
location.protocol //返回页面使用的协议
location.hash="#top"
location.href="index.html"   //跳转到index.html页面,并且生成历史记录
window.location="index.html"   //location.href==  window.location
location.replace("index.html")//重定向url  不会产生历史记录

history对象

history.back()   //返回到历史纪录的上一步
history.go(-1)  //和上面一样  -2返回历史记录前两部
history.forward() //返回历史记录的下一步
history.go(1)    //和上面一样

screen对象

screen.availWidth    //获取屏幕宽度
screen.availHeight   //获取屏幕高度
window.innerWidth    //获取窗口的宽度
window.innerHeight   //获取窗口的高度

Navigator对象
判断浏览器的类型
判断设备的终端是移动还是PC

//封装一个检测浏览器类型的函数
navigator.userAgent   //用来识别浏览器名称版本操作系统等内容
function getBrowser(){
	var export=navigator.userAgent.toLowerCase(),browser;
	if(export.indexOf("msie")>-1){
		browser="IE"
	}else if(export.indexOf("chrome")>-1){
    	browser="chrome"
    }else if(export.indexOf("opera")>-1){
		browser="opera"
	}else if(export.indexOf("safari")>-1){
		browser="safari"
	}
	return browser
}
发布了25 篇原创文章 · 获赞 0 · 访问量 640

猜你喜欢

转载自blog.csdn.net/JamesHKK/article/details/104527254