1.窗口位置
以下取得浏览器窗口距屏幕左边和上边的位置
实现代码如下:

var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX; //左边位置
var topPos = (typeof window.screenTop == "number") ? window.screenTop : window.screenY; //上边位置

2.浏览器大小
以下取得浏览器页面视口的大小
实现代码如下:

var pageWidth = window.innerWidth,
pageHeight = window.innerHeight;

if (typeof pageWidth != "number") {
if (document.compatMode == "CSS1Compat") {
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
} else {
pageWith = document.body.clientWdith;
pageHeight = document.body.clientHeight;
}
}

3.打开或弹出窗口
window.open()方法,可以接受4个参数,通常只需指定第一个参数,第一个参数为URL,第二个参数为_self 、_parent 、_top 、_blank 或者框架名
实现代码如下:

window.open("http://www.baidu.com");
window.open("http://www.baidu.com","_blank");
window.open("http://www.baidu.com","topFrame","height=400,width=400,top=10,left=10,resizable = yes");
topFrame.resizeTo(500,300); //调整窗口大小
topFrame.moveTo(100,100); //移动窗口位置
topFrame.close(); //关闭新打开的窗口,IE会报错

4.location 对象
location.href(URL) 加载URL
实现代码如下:

location.href(URL) 加载URL
location.href("http://www.baidu.com");
location.href = "http://www.baidu.com" ; //同上
location.assign = "http://www.baidu.com"; //同上
window.loaction = "http://www.baidu.com"; //同上
location.replace("http://www.baidu.com"); //同上,但不能回退

location.reload(); //重新加载(可能从缓存中加载)
location.reload(true); //重新加载(从服务器中加载)

location.search() 返回URL中的查询字符串,字符串以为?开头

5.获取查询字符串参数
实现代码如下:

function getQueryStringArgs() {
var qs = (location.search.length > 0) location.search.substring(1) : "";
var args ={};
var items = qs.split("&");
var item = null,name = null,value = null;
for (var i=0 ; i<items.length ; i++)
{
item = itmes[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
args[name] = value;
}
return args;
}

//假设查询字符串参数是?q=javascript&num=10
var args = getQueryStringArgs();
alert(args["q"]); //"javascript"
alert(args["num"]); //"10"

6.history 对象
实现代码如下:

history.go()页面跳转
history.go(-1); //后退一页
history.go(1); //前进一页
history.go(2); //前进两页
history.go("baidu.com"); 跳转到最近的baidu.com页面

history.back(); //后退一页
history.forword(); //前进一页

检测当前页是不是用户打开的第一个页面
实现代码如下:

if (history.length == 0) {
//如果打开的是第一个页面的话,执行某些操作
}

7.页面加载
window.onload() 用于页面加载结束后做某些操作
实现代码如下:

window.onload = function () {
//执行某些操作
}

以上就是【JavaScript学习日志(十四) window对象使用介绍】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论