前言
上一篇博客介绍了DOM,那么这篇博客就介绍一下BOM(浏览器对象),这也是学习JavaScript必学的一门知识。
内容
字面值:
在代码中写入的是什么显示出来的就是什么
window相当于当前浏览器窗口
使用window对象属性、方法时可以省略window(节约字节数)
例如alert、document等
confirm方法:
出现确定或取消框
bool confirm(“提示字符串”);
eg.
<script type="text/javascript">
onload = function(){
btn.onclick = function(){
var res =window.confirm("测试消息,请选择");
alert(res);
}
}
</script>
<body>
<input type="button" name="name" value="click" id="btn"/>
</body>
window.location属性
window.location.href="url";:页面的跳转
window.location.reload();
screen对象
获取屏幕分辨率:width与height
history对象
back方法与forward方法
go(-1)与go(1)
eg.
**page1**
<script type="text/javascript">
onload = function(){
btn.onclick = function(){
//history.forward();
history.go(1);
}
}
</script>
<body>
<a href=page2.html>第二页</a><br />
<input type="button" name="name" value="->" id="btn"/>
</body>
**page2**
<script type="text/javascript">
onload = function(){
btn.onclick = function(){
//history.back();
history.go(-1);
}
}
</script>
<body>
<input type="button" name="name" value="<-" id="btn"/>
</body>
兼容性的选择
火狐浏览器:
var dv = document.getElementById("dv");
dv.onmousedown = function€{
alert(e);
}
ie浏览器:
var dv =document.getElementById("dv");
dv.onmousedown = function(){
alert(window.event.button);
}
合并的写法:
var dv = document.getElementById("dv");
dv.onmousedown = function(e){
//早期的ie中,e是undefined
e = e || window.event;
//如果e不存在,就用event,这个不是标准做法
//如果e存在就是用e本身
alert(e.button);
}
功能键shift、alt、control
onload = function(){
window.btn.onclick = function€{
//shift、alt、control
e = e || event
alert("alt=" + e.altKey + ",shift=" + e.shiftKey + ",control=" + e.ctrlKey);
}
}
总结
颗粒归仓。
end
谢谢您的阅读!