您的位置:首页 > Web前端 > JavaScript

<JavaScript高级程序设计>读书笔记(第8章BOM之window对象)

2016-11-01 00:00 483 查看
1.全局变量不能通过delete操作符删除,而直接在window对象上定义的对象可以

var age=19;//全局变量
window.color="red";

delete window.age;//在IE<9时抛出错误,在其他浏览器中返回false
delete window.color;//在IE<9时抛出错误,在其他浏览器中返回true

console.log(window.age);//19
console.log(window.color);//undefined

2.获取窗体大小

var pageWidth=window.innerWidth,
pageHeight=window.innerHeight;
if(typeof pageWidth !="number"){
if(document.compatMode=="CSS1Compat"){
pageWidth=document.documentElement.clientWidth;
pageHeight=document.documentElement.clientHeight;
}else{
pageWidth=document.body.clientWidth;
pageHeight=document.body.clientHeight;
}
}
console.log(pageWidth);
console.log(pageHeight);

3.打开新窗口

window.open("test.html","_blank");

4.检测是否屏蔽了弹窗

//IE(IE7、8)和火狐会返回null
//chrome不返回null
if(openWin == null){
alert("您的浏览器屏蔽了弹窗");
}

5.更准确的检测浏览器是否屏蔽了弹窗

//IE(IE7、8)和火狐会返回null,chrome不返回null
var blocked=false;
try{
var openWinTest=window.open("demoLayout-1.html");
if(openWinTest==null){
blocked=true;
}
}catch(ex){
blocked=true;
}

if(blocked){
alert("您的浏览器屏蔽了弹窗");
}

6.超时调用

setTimeout(function(){
alert("hello");
},1000);

//超时调用的取消
var timeoutId=setTimeout(function(){
alert("Hello");
},1000);
//取消
clearTimeout(timeoutId);

7.间歇调用,会根据设置的时间值来重复调用,直到被取消或页面被卸载

setInterval(function(){
alert("Hello");
},1000);

//间歇调用取消
var intervalId=setInterval(function(){
alert("Hello");
},1000);
clearInterval(intervalId);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐