什么是BOM
BOM(browser odject model)浏览器对象模型
BOM对象
window
所有的全局变量和全局方法都被归在window上
window对象方法
1、超时调用setTimeOut(code,mollisec)
说明:setTimeout()只执行code一次,如果要多次调用,可以让code自身再次调用setTimeout()
<script type="text/javascript">
var fnCall=function(){
alert("world");
}
var timeout=setTimeout(function(){
alert("hello");
},3000);
setTimeout(fnCall,6000);
clearTimeout(timeout);
</script>
2、间歇调用
JavaScript是单线程语言,单线程就是所执行的代码必须按照顺序
var intervalId=setInterval(function(){
console.log("setInterval");
},2000);
setTimeout(function(){
clearInterval(intervalId);
},10000);
var num=1,
max=10,
timer=null;
// timer=setInterval(function(){
// console.log(num);
// num++;
// if(num > max){
// clearInterval(timer);
// }
// },1000);
//使用超时调用实现
function inCreamentNum(){
console.log(num);
num++;
if(num <= max){
setTimeout(inCreamentNum,1000);
}else{
clearTimeout(timer);
}
}
timer=setTimeout(inCreamentNum,1000);
navigator
1、掌握navigator对象的userAgent属性
2、掌握如何判断浏览器的类型
3、掌握如何判断设备的终端是移动还是pc
appCodeName:获取浏览器名称
appName:获取完整的浏览器名称
console.log(navigator.userAgent);
<script type="text/javascript">
// console.log(navigator.userAgent);
function getBrowser(){
var explorer = navigator.userAgent.toLowerCase(),
browser;
if(explorer.indexof("msie")>-1){
browser = "IE";
}else if(explorer.indexof("chrome")>-1){
browser = "chrome";
}else if(explorer.indexof("opera")>-1){
browser = "opera";
}else if(explorer.indexof("safari")>-1){
browser = "safari";
}
return browser;
}
var explorer1 = getBrowser();
alert("您当前使用的是:"+explorer1+"浏览器");
</script>
screen
screen对象包含有关客户端显示屏幕的信息
<script type="text/javascript">
console.log("页面高:"+screen.availWidth);
console.log("页面宽:"+screen.availHeight);
console.log("pageHeight:"+window.innerHeight);
console.log("pageWidth:"+window.innerWidth);
</script>
history
history对象保存了用户在浏览器中访问页面的历史记录