javascript学习笔记(十四)window对象
1.窗口位置
以下取得浏览器窗口距屏幕左边和上边的位置
1 var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX; //左边位置
2 var topPos = (typeof window.screenTop == "number") ? window.screenTop : window.screenY; //上边位置
2.浏览器大小
以下取得浏览器页面视口的大小
1 var pageWidth = window.innerWidth,
2 pageHeight = window.innerHeight;
3
4 if (typeof pageWidth != "number") {
5 if (document.compatMode == "CSS1Compat") {
6 pageWidth = document.documentElement.clientWidth;
7 pageHeight = document.documentElement.clientHeight;
8 } else {
9 pageWith = document.body.clientWdith;
10 pageHeight = document.body.clientHeight;
11 }
12 }
3.打开或弹出窗口
window.open()方法,可以接受4个参数,通常只需指定第一个参数,第一个参数为URL,第二个参数为_self 、_parent 、_top 、_blank 或者框架名
1 window.open("http://www.2cto.com");
2 window.open("http://www.2cto.com","_blank");
3 window.open("http://www.baidu.com","topFrame","height=400,width=400,top=10,left=10,resizable = yes");
4 topFrame.resizeTo(500,300); //调整窗口大小
5 topFrame.moveTo(100,100); //移动窗口位置
6 topFrame.close(); //关闭新打开的窗口,IE会报错
4.location 对象
location.href(URL) 加载URL
1 location.href(URL) 加载URL
2 location.href("http://www.2cto.com");
3 location.href = "http://www.2cto.com" ; //同上
4 location.assign = "http://www.2cto.com"; //同上
5 window.loaction = "http://www.2cto.com"; //同上
6 location.replace("http://www.2cto.com"); //同上,但不能回退
7
8 location.reload(); //重新加载(可能从缓存中加载)
9 location.reload(true); //重新加载(从服务器中加载)
10
11 location.search() 返回URL中的查询字符串,字符串以为?开头
5.获取查询字符串参数
1 function getQueryStringArgs() {
2 var qs = (location.search.length > 0) location.search.substring(1) : "";
3 var args ={};
4 var items = qs.split("&");
5 var item = null,name = null,value = null;
6 for (var i=0 ; i<items.length ; i++)
7 {
8 item = itmes[i].split("=");
9 name = decodeURIComponent(item[0]);
10 value = decodeURIComponent(item[1]);
11 args[name] = value;
12 }
13 return args;
14 }
15
16 //假设查询字符串参数是?q=javascript&num=10
17 var args = getQueryStringArgs();
18 alert(args["q"]); //"javascript"
19 alert(args["num"]); //"10"
6.history 对象
1 history.go()页面跳转
2 history.go(-1); //后退一页
3 history.go(1); //前进一页
4 history.go(2); //前进两页
5 history.go("baidu.com"); 跳转到最近的baidu.com页面
6
7 history.back(); //后退一页
8 history.forword(); //前进一页
检测当前页是不是用户打开的第一个页面
1 if (history.length == 0) {
2 //如果本页面是用户打开的第一个页面的话
3 }
摘自 晴天漫步