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

JS高级程序设计8-BOM

2015-07-16 15:28 501 查看
window对象(P193)

定义全局变量与在window对象上直接定义属性的差别:

var age=29;
window.color="red";
delete window.age;//返回false
delete window.color;//返回true
alert(window.age);//29
alert(window.color);//undefind


尝试访问未声明的变量会抛出错误,但通过查询window对象,可以知道某个可能未声明的变量是否存在

var newValue=oldValue;//抛出错误,因为oldValue没有定义
var newValue=window.oldValue; //undefind,因为是一次属性查询


窗口位置(P197)

兼容获取窗口的位置的方法:
var leftPos=(typeof window.screenLeft=="number")?window.screenLeft:window.screenX;
var topPos=(typeof window.screenTop=="number")?window.screenTop:window.screenY;

moveTo()和moveBy()方法可以将窗口精确的移动到一个新位置


窗口大小(P198)

兼容获取窗口大小的方法:
var pageWidth=window.innerWidth,pageHeight=window.innerheight;
if(typyof pageWidth!="number"){
if(document.compatMode=="CSS1Compat"){
pageWidth=document.documentElement.clientWidth;
pageHeight=document.documentElement.clientHidth;
}else{
pageWidth=document.body.clientWidth;
pageHeight=document.body.clientHeight;
}
}else{
pageWidth=document.documentElement.clientWidth;
pageHeight=document.documentElement.clientHeight;
}

resizeBy()和resizeTo()方法可以调整浏览器窗口的大小


导航和打开窗口(弹出窗口)(P199)

window.open(要加载的URL,窗口目标,一个特性字符串,表示新页面是否取代浏览器历史记录中当前加载页面的布尔值);

window.open()方法会返回一个指向新窗口的引用,通过这个引用可以控制新创建的窗口:
var wroxWin=window.open("http://www.dfs.com/","xroWindow","height=400,width=400,top=10,left=10,resizable=yes");
wroxWin.resizeTo(500,500);//调整大小
wroxWin.moveTo(100,100);//移动位置

//调用close()方法还可以关闭新打开的窗口
wroxWin.close();

//新创建的window对象有一个opener属性,其中保存着打开它的原始窗口对象
wroxWin=window.open("http://www.dfs.com/","xroWindow","height=400,width=400,top=10,left=10,resizable=yes");
alert(wroxWin.opener==window); //true

//把opener设置为null可以使新创建的窗口与打开它的原标签页断开联系,使新创建的窗口可以在单独的进程中运行


间歇调用和超时调用(P203)

超时调用---setTimeout()
//设置超时调用
var timeoutId=setTimeout(function(){
alert("Hello world!");
},1000);
//注意,把它取消
clearTimeout(timeoutId);

间歇调用---setInterval()
var num=0;
var max=10;
var intervalId=null;
function incrementNumber(){
num++;
//如果执行次数达到了max设定的值,则取消后续尚未执行的调用
if(num==max){
clearInterval(intervalId);
alert("Done");
}
}
intervalId=setinterval(incrementNumber,500);


系统对话框(P205)

有alert()、confirm()和prompt()
alert()---警告

confirm()---确认/取消
if(confirm("Are you sure?")){
alert("I'm so glad you're sure!");
}else{
alert("I'm sorry to hear you're not sure.");
}

prompt()---提示/输入框/确认/取消


location对象(P207)

location既是window对象的属性,也是document对象的属性,window.location和document.location都可以。功能:解析URL&保存当前文档信息

//查询字符串参数

//位置操作(下面三种效果相同)
location.assign("http://www.wrox.com");
window.location="http://www.wrox.com";
location.href="http://www.wrox.com";
//location对象娶她的属性也可以改变当前加载的页面,并且返回会回到当前页
//但location的replace()方法有所区别,它用新的页面替换原有页面(不能返回到当前页)
reload()方法//重新加载当前页
location.reload(); //重新加载(有可能从缓存中加载)
lication.reload(true); //重新加载(从服务器重新加载)


navigator对象(P211)

检查插件

function hasPlugin(name){
name=name.toLowerCase();
for(var i=0;i<navigator.plugins.length;i++){
if(navigator.plugins[i].name.tolowerCase().indexOf(name)>-1){
return true;
}
}
}
//检测Flash
alert(hasPlugin("Flash"));
//检测QuickTime
alert(hasPlugin("QuickTime"));


注册处理程序(P213)

registerContenthandler()//可以将一个站点注册为处理某种文件的处理程序
registerProtocolHandler()//可以将一个站点注册为处理某种协议的处理程序


screen对象(P214)

history对象(P215)

跳转页码

//后退一页
history.go(-1);
//前进两页
history.go(2);
//后退一页
history.back();
//前进一页
history.forward();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: