BOM(Browser Object Model)-浏览器对象模型
1. window 对象
window
对象扮演着ECMAScript
中Global
对象的角色,因此全局声明的对象或者方法会变成window
对象的属性和方法
- 窗口的可视区域大小
- 打开新窗口
window.open("http://www.baidu.com")
- 定时器
// 超时调用
var timeoutId = setTimeout(function(){
console.log("定时器");
}, 1000)
clearTimeout(timeoutId); // 提前取消定时器
// 间歇调用
var num = 0;
var interval = setInterval(function(){
num++;
console.log("间歇调用");
if (num === 5){
clearInterval(interval);
console.log("Done");
}
}, 500)
- 弹出窗口
alert("alert")
confirm("are you sure"); // true | false
prompt("input name:", "mak"); // 返回输入的值, 第二个参数为默认值
2. location 对象
- 属性
- 解析查询参数字符串
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;
// 逐个将每一项目添加到args对象中
for (i; i < len; i++) {
item = items[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(items[1]);
if (name.length) {
args[name] = value
}
}
console.log(args);
return args;
}
- 方法
// 下面两条语句效果相同
window.location.assign("http://www.baidu.com")
location.href = "http://www.baidu.com"
// 跳转页面,但是不会保存历史记录(不能点击回退按钮)
location.replace("http://www.baidu.com")
// 重载页面
location.reload() // 优先缓存中加载
location.reload(true) // 从服务器重新获取
3. navigator 对象
该对象包含有关浏览器的信息,具体方法见 w3school
4. Screen 对象
该对象包含有关客户端显示屏幕的信息,具体方法见 w3school
5. history 对象
// 后退
history.back()
history.go(-1)
// 前进
history.forward()
history.go(1)
// 前进2页
history.go(2)
// 跳转到最近的百度界面
history.go("www.baidu.com")