javascript高级程序设计笔记-第八章(BOM)

一、window对象

1、全局作用域

所有在全局作用域声明的变量和函数,都变成window对象的属性和方法

全局变量不能通过delete操作符删除,而直接在window对象上定义的属性可以

var age = 29;
window.color = "red";
delete window.age;
delete window.color;
alert(window.age); //29
alert(window.color); //undefined

2、框架

每个框架都有自己的window对象,并且保存在frames集合中,可以通过数值索引或框架名称访问相应的window对象

top对象始终指向最外层的框架

parent对象始终指向当前框架的直接上层框架(父框架)

3、窗口大小

//取得视口大小
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;
}
}

resizeTo()、resizeBy()方法可以调整浏览器窗口的大小,接收两个参数(一个宽度、一个高度)

4、弹出窗口 ###

window.open()方法,可以导航到一个特定的URI,也可以打开一个新的浏览器窗口。
接收四个参数:要加载的URI、窗口目标、一个特性字符串、一个布尔值。(一般只使用第一个参数)

5、间歇调用和超时调用

超时调用setTimeout()方法,接收两个参数:要执行的代码(函数)和以毫秒表示的时间
clearTimeout()方法,取消超时调用,接收一个参数:超时调用ID

间歇调用setInterval()方法,clearInterval()方法(最好不要使用间歇调用)

一般使用超时调用来模拟间歇调用

var num = 0;
var max = 10;
var intervalId = null;
function incrementNumber() {
num++;
if (num < max) {
setTimeout(incrementNumber, 500);
} else {
alert("done");
}
}
setTimeout(incrementNumber, 500);

6、系统对话框

它们的外观又操作系统或浏览器设置决定,不能由css决定。打开的对话框都是同步和模态的

  • alert(),生成警告对话框,向用户显示一些他们无法控制的信息
  • confirm(),生成确认对话框,让用户决定是否执行给定的操作
  • prompt(),生成提示框,用于提示用户输入一些文本

二、location对象

提供与当前窗口中加载的文档有关的信息,还可以将URI解析为独立的片段

查询字符串

function getQueryStringArgs() {
//取得字符串并去掉开头的问号
var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
//保存数据的对象
args = {},
//取得每一项
items = qs.length ? qs.split("&") : [],
item = null,
name = null,
value = null,
//for循环中使用
i = 0,
len = items.length;
for (i = 0; i < len; i++) {
item = items[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
if (name.length) {
args[name] = value;
}
}
return args;
}

三、检测插件

//检测非IE浏览器中的插件
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;
}
}
return false;
}
//检测IE中的插件
function hasIEPlugin(name) {
try {
new ActiveXObject(name);
return true;
} catch (ex) {
return false;
}
}

针对每个插件分别创建检测函数

//检测所有浏览器中的Flash
function hasFlash() {
var result = hasPlugin("Flash");
if (!result) {
result = hasIEPlugin("ShockwaveFlash.ShockwaveFlash");
}
return result;
}
//检测所有浏览器中的QuickTime
function hasQuickTime() {
var result = hasPlugin("QuickTime");
if (!result) {
result = hasIEPlugin("QuickTime.QuickTime");
}
return result;
}

posted @ 2016-03-03 13:29  u14e  阅读(215)  评论(0)    收藏  举报