var age=29; function sayAge(){ alert(this.age); } alert(window.age);//29 sayAge();//29 window.sayAge();//29
全局变量不能经过delete操做符删除,而直接在window对象上的定义的属性能够javascript
var age=29; window.color="red"; //在IE<9时抛出错误,在其余全部浏览器中都返回false delete window.age; //在IE<9时抛出错误,在其余全部浏览器中都返回true delete window.color;//return true alert(window.age);//29 alert(window.color);//undefined
尝试访问未声明的变量会抛出错误,可是经过查询window对象,能够知道某个可能未声明的变量是否存在html
//这里会抛出错误,由于oldValue未定义 var newValue=oldValue //这里不会抛出错误,由于这是一次属性查询 //newValue的值是undefined var newValue=window.oldValue
若是页面中包含框架,则每一个框架都拥有本身的window对象,而且保存在frames集合中java
<html> <head> <title>Frameset Example</title> </head> <frameset rows="160,*"> <frame src="frame.htm" name="topFrame"> <frameset cols="50%,50%"> <frame src="anotherframe.htm" name="leftFrame"> <frame src="yetanotherframe.htm" name="rightFrame"> </frameset> </frameset> </html>
screenLeft()和screenTop()属性,分别用于表示窗口相对于屏幕左边和上边的位置数组
var leftPos=(typeof window.screenLeft=="number")?window.screenLeft:window.screenX; var topPos=(typeof window.screenTop=="number")?window.screenTop:window.screenY;
moveTo()接收的是新位置的x和y的坐标值,而moveBy()接收的是在水平和垂直方向上移动的像素数浏览器
//将窗口移动到屏幕左上角 window.moveTo(0,0); //将窗口向下移动100像素 window.moveBy(0,100); //将窗口移动到(200,300) window.moveTo(200,300); //将窗口向左移动50像素 window.moveBy(-50,0)
使用resizeTo()和resizeBy()方法能够调整浏览器窗口的代销。这两个方法都接收两个参数,其中resizeTo()接收浏览器窗口的新宽度和高度,而resizeBy()接收新窗口与原窗口的宽度和高度之差。框架
//调整到100X100 window.resizeTo(100,100); //调整到200X150 window.resizeBy(100,50) //调整到300X300 window,resizeTo(300,300)
confirm()方法,像是一个"警告"对话框。除了显示"肯定"按钮以外,还会显示一个Cancel("取消")按钮,两个按钮可让用户决定是否执行给定的操做函数
if (confirm("Are you sure?")) { alert("I'm so glad you're sure! "); } else { alert("I'm sorry to hear you're not sure. "); }
建立函数,解析查询字符串,返回包含全部参数的一个对象工具
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=0; i < len; i++){ item = items[i].split("="); name = decodeURIComponent(item[0]); value = decodeURIComponent(item[1]); if (name.length) { args[name] = value; } } return args; }
使用 location 对象能够经过不少方式来改变浏览器的位置。首先,也是最经常使用的方式,就是使用assign() 方法并为其传递一个 URL。ui
location.assign("http://www.wrox.com");
下面两行代码与显式调用assign()方法的效果彻底同样this
window.location = "http://www.wrox.com"; location.href = "http://www.wrox.com";
//假设初始 URL 为 http://www.wrox.com/WileyCDA/ //将 URL 修改成"http://www.wrox.com/WileyCDA/#section1" location.hash = "#section1"; //将 URL 修改成"http://www.wrox.com/WileyCDA/?q=javascript" location.search = "?q=javascript"; //将 URL 修改成"http://www.yahoo.com/WileyCDA/" location.hostname = "www.yahoo.com"; //将 URL 修改成"http://www.yahoo.com/mydir/" location.pathname = "mydir"; //将 URL 修改成"http://www.yahoo.com:8080/WileyCDA/" location.port = 8080
非IE浏览器可使用plugins数组来达到这个目的
//检测插件(在 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; } //检测 Flash alert(hasPlugin("Flash")); //检测 QuickTime alert(hasPlugin("QuickTime"));
使用 go() 方法能够在用户的历史记录中任意跳转,能够向后也能够向前。这个方法接受一个参数,表示向后或向前跳转的页面数的一个整数值。负数表示向后跳转(相似于单击浏览器的“后退”按钮),正数表示向前跳转(相似于单击浏览器的“前进”按钮)。
//后退一页 history.go(-1); //前进一页 history.go(1); //前进两页 history.go(2);
也能够给 go() 方法传递一个字符串参数,此时浏览器会跳转到历史记录中包含该字符串的第一个位置——可能后退,也可能前进,具体要看哪一个位置最近。若是历史记录中不包含该字符串,那么这个方法什么也不作,
//跳转到最近的 wrox.com 页面 history.go("wrox.com"); //跳转到最近的 nczonline.net 页面 history.go("nczonline.net");
还可使用两个简写方法 back() 和 forward() 来代替 go() 。
//后退一页 history.back(); //前进一页 history.forward();
history 对象还有一个 length 属性,保存着历史记录的数量。
if (history.length == 0){ //这应该是用户打开窗口后的第一个页面 }