Window对象
窗口大小
1
2
3
|
outerWidth 和 outerHeight返回浏览器窗口自身的大小
innerWidth 和 innerHeight返回浏览器窗口中页面视口的大小(不包括浏览器边框和工具栏)
document.documentElement.clientWidth 和 document.documentElement.clientHeight 返回页面视口的宽高
|
确定页面视口大小
1
2
3
4
5
6
7
8
9
10
11
12
|
let pageHeight = window.innerHeight;
let pageWidth = window.innerWidth;
if(typeof pageHeight != "number") {//检查页面是否处于标准模式
if(document.compatMode == "CSS1Compat") {//移动设备浏览器兼容处理
pageHeight = document.documentElement.clientHeight;
pageWidth = document.documentElement.clientWidth;
} else {
pageHeight = document.body.clientHeight;
pageWidth = document.body.clientWidth;
}
}
|
缩放方法
1
2
3
|
window.resizeTo(x, y);缩放到
window.resizeBy(x, y);缩放+
可能被浏览器禁用
|
视口位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
window.scrollBy(x, y);相对当前视口滑动
window.scrollTo(x, y);滚到据屏幕左边x顶边y的位置
平滑活动设置 behavior
正常滚动:
window.scrollBy({
left: x,
top: y,
behavior: 'auto'
});
平滑滑动
window.scrollBy({
left: x,
top: y,
behavior: 'smooth'
});
|
导航与打开新窗口
window.open(URL、目标窗口、特性字符串、表示新窗口在浏览器历史记录中是否替代当前加载页的bool值),通常只用前三个参数,最后一个只有在不打开新窗口时才会使用
1
2
|
window.open("http://www.wrox.com","topFrame"); 等价于
<a href="http://www.wrox.com" target="topFrame"></a>相同
|
特性字符串
设置 |
值 |
说明 |
fullscreen |
yes 或 no |
表示窗口是否最大化,仅IE支持 |
height |
|
值必须大于等于100 |
left |
|
x轴坐标 |
location |
|
是否显示地址栏,各浏览器默认不同 |
Menubar |
yes 或 no |
是否显示菜单栏,默认为no |
等等
一个例子
1
2
|
window.open("http://www.wrox.com","topFrame",
"height=400, width=400, top=10, left=10, resizeable=yes");
|
window.close
只能用于关闭window.open()创建的窗口,关闭窗口后,窗口的引用虽然还在,但只能用于检查其**closed(是否已关闭)**属性。
window.opener
指向打开它的窗口的指针,将其设为null,表示新打开的标签页可以运行在独立的进程中。
弹窗屏蔽程序
检查弹窗是否被屏蔽
如果被屏蔽,window.open()很可能返回null
1
2
3
4
|
let wrox = window.open("http://www.wrox.com","topFrame");
if(wrox == null) {
alert("the popup was blocked!");
}
|
但实际上,在浏览器扩展或其他程序屏蔽弹窗时window.open()通常会抛出错误。因此要准确检测弹窗是否被屏蔽,还要用try/catch包装起来
1
2
3
4
5
6
7
8
9
10
11
12
|
let blocked = false;
try {
let wrox = window.open("http://www.wrox.com","topFrame");
if(wrox == null) {
blocked = true;
}
} catch(ex) {
blocked = true;
}
if(blocked) {
alert("the popup was blocked!");
}
|
location对象
查询字符串
1
2
3
4
5
6
7
8
9
10
11
|
let getQueryStringArgs = function() {
let qs = (location.search.length > 0 ? location.search.substring(1) : "");
let args = {};
for(let item of qs.split("&").map(kv => kv.split("="))) {
let name = item[0];
let value = item[1];
if(name.length) {
args[name] = value;
}
}
}
|
URLSearchParams支持检查和修改查询字符串,给其传入一个查询字符串就可以创造一个实例,该实例上有get(), set(), has(), delete()等方法
修改地址
href,assign
1
2
3
4
5
|
location.href = "http://www.wrox.com";
location.assign("http://www.wrox.com");
window.location = "http://www.wrox.com";
修改location的属性也会修改当前加载的页面
tips:修改hash的值会在浏览器历史中增加一条新纪录
|
replace
若不希望增加历史记录,可使用location.replace(),但调用后用户不能回到前一页
reload()
1
2
|
location.reload(); 重新加载,可能从缓存加载
location.reload(true); 重新加载,从服务器加载
|
navigator对象
检测插件
IE11及以上可用plugins数组确定,包括一下属性
1
2
3
4
|
name 插件名称
description 插件介绍
filename 插件的文件名
length 由当前插件处理的MIME类型数量
|
插件检测
1
2
3
4
5
6
7
8
9
|
let hasPlugin = function(name) {
name = name.toLowerCase();
for(let plugin of window.navigator.plugins) {
if(plugin.name.toLowerCase().indexOf(name) > -1){
return true;
}
}
return false;
}
|
注册处理程序
registerProtocolHandler();
screen对象
显示器信息
history对象
导航
1
2
3
4
5
|
history.go(2) 前进2页
history.go(-2) 后退2页
history.forward() 前进1页
history.back() 后退1页
history.length 表示历史记录中有多少个条目
|
历史状态管理 ???