

实例:点击按钮,在新窗口打开百度首页,宽600,高400,距屏顶0像素,屏左10像素。
<body> <input type="button" onClick="newWin()" value="点击我,打开新窗口!"> <script> function newWin(){ window.open("http://www.baidu.com", "_blank", "width=,height=,top=,left="); } </script> </body> 该实例在IE下并不会打开一个自定义的窗口,而是新打开一个标签页。<script> window.open();window.open("http://baidu.com"); </script>实例:获得焦点和失去焦点
<body> <input type="button" value="获得焦点" onclick="openWin()"> <script> //确保新的窗口获得焦点: function openWin(){ var oGet=window.open("", "", "width=,height="); oGet.document.write("<p>我是新打开的窗口</p>"); oGet.focus(); } </script> <input type="button" value="失去焦点" onclick="newWin()"> <script> //确保新的窗口没有获得焦点: function newWin(){ var lose=window.open("", "", "width=,height="); lose.document.write("<p>我是新打开的窗口</p>"); lose.blur(); } </script> </body> 实例:返回新打开窗口的名称 <body> <input type="button" value="打开" onclick="openWin()"> <script> function openWin(){ var newWin=window.open("", "newWindow", "width=,height="); newWin.document.write("<p>新窗口名称:"+ newWin.name + "</p>"); } </script> </body> 实例:打开新窗口向父窗口传递信息 <body> <input type="button" value="打开" onclick="openWin()"> <script> function openWin(){ var newWin=window.open("", "", "width=,height="); newWin.document.write("<p>关闭我之后会向父窗口输入文本信息</p>"); newWin.focus(); newWin.opener.document.write("<p>我是父窗口,加载完成后,向我输出内容会覆盖我所有内容</p>"); } </script> </body> 实例:指定窗口大小、移动窗口和滚动内容 <!DOCTYPE html> <html> <head> <meta charset="UTF-"> <title>JavaScript实例</title> </head> <body> <br><br> <h>用指定像素调整窗口大小:</h> <input type="button" value="打开" onclick="openWin()"><br><br> <input type="button" value="调整" onclick="byWin()"> <script> var w; function openWin(){ w=window.open("", "", "width=,height=,top="); w.focus(); } function byWin(){ //如果不使用框架,可使用window代替top w.top.resizeBy(,); w.focus(); } </script> <h>将窗口的大小调整到指定的宽度和高度:</h> <input type="button" value="打开" onclick="newWin()"><br><br> <input type="button" value="调整" onclick="toWin()"> <script> var w; function newWin(){ w=window.open("", "", "width=,height="); w.focus(); } function toWin(){ w.resizeTo(,); w.focus(); } </script> <h>相对于当前位置移动新窗口:</h> <input type="button" value="打开" onclick="oWin()"><br><br> <input type="button" value="多移动几下" onclick="moveWin()"> <script> var w; function oWin(){ w=window.open("", "", "width=,height="); } function moveWin(){ w.moveBy(,); w.focus(); } </script> <h>移动新窗口到指定位置:</h> <input type="button" value="打开" onclick="nWin()"><br><br> <input type="button" value="移动" onclick="moveToWin()"> <script> var w; function nWin(){ w=window.open("", "", "width=,height="); } function moveToWin(){ w.moveTo(,); w.focus(); } </script> <h>由指定的像素数滚动内容:</h> <input type="button" style="position:fixed;top:;" onclick="scrollWin()" value="由指定的像素数滚动内容"> <script> function scrollWin(){ window.scrollBy(,); } </script> <h>滚动到指定内容处:</h> <input type="button" onclick="scrollWindow()" value="滚动到指定内容处"> <script> function scrollWindow(){ window.scrollTo(,); } </script> <br><br><br><br><br><br> </body> </html> 方法解析: <body> <input type="button" value="打印当前页面" onclick="printpage()"> <script> function printpage(){ window.print(); } </script> </body> (2)、关闭窗口。//点击按钮关闭当前窗口 <input type="button" value="关闭窗口" onclick="window.close()">该方法在Chrome下运行正常。IE下弹窗提示:你查看的网页正在试图关闭选项卡,是否关闭选项卡?点击否,不关闭,点击是,关闭窗口。在FF下会报错。因为在FF下不允许脚本关闭非脚本打开的窗口,也就是不能关闭一个用户自己打开的窗口,只能关闭由open打开的窗口。所以可以先用open打开,再关闭,这样就能解决FF下不能关闭的问题。这就需要创建两个文档来演示该问题:第二个文档使用上面实例保存为close.html,第一个文档如下:
//先用open打开保存的文档,然后再点击关闭窗口按钮,效果就达到了 <input type="button" value="打开窗口" onclick="window.open("close.html");">FF浏览器的安全机制比较高,不能关闭用户打开的窗口,在网上也没找有找到什么好的办法,只能修改浏览器的默认配置,显然这是不可取的。上面的办法比较笨,另辟蹊跷不能关闭用户打开的,那就自己open一个再close,但这还是比较实在的方法,至少目的是达到了。
<a href="javascript:window.opener=null;window.open("", "_self");window.close();">关闭</a>实例:关闭新打开的窗口
<body> <input type="button" value="打开" onclick="openWin()"> <input type="button" value="关闭" onclick="closeWin()"> <script> function openWin(){ newWin=window.open("http://www.baidu.com", "", "width=,height=,top="); } function closeWin(){ newWin.close(); } </script> </body> 实例:检查新窗口是否已关闭<body> <input type="button" value="打开"" onclick="openWin()"> <input type="button" value="关闭" onclick="closeWin()"> <input type="button" value="是否关闭" onclick="checkWin()"> <p id="p"></p> <script> var newWin; function openWin(){ newWin=window.open("", "", "width=,height=,top="); } function closeWin(){ if(newWin){ newWin.close(); } } var oP=document.getElementById("p"); function checkWin(){ if(!newWin){ oP.innerHTML="新窗口还没被打开!"; } else{ if(newWin.closed){oP.innerHTML="新窗口已关闭!"; } else{ oP.innerHTML="新窗口未关闭!"; } } } </script> </body><body> <div id="div"></div> <script> txt = "<p>Browser CodeName(浏览器代码名称): " + navigator.appCodeName + "</p>"; txt+= "<p>Browser Name(浏览器名称): " + navigator.appName + "</p>"; txt+= "<p>Browser Version(浏览器版本): " + navigator.appVersion + "</p>"; txt+= "<p>Cookies Enabled(启用Cookies): " + navigator.cookieEnabled + "</p>"; txt+= "<p>Platform(操作平台): " + navigator.platform + "</p>"; txt+= "<p>User-agent header(由客户机发送服务器的 user-agent 头部信息): " + navigator.userAgent + "</p>"; txt+= "<p>User-agent language(客户机代理语言): " + navigator.systemLanguage + "</p>"; document.getElementById("div").innerHTML=txt; </script> </body>其中最常用的属性是navigator.userAgent,返回用户代理头的字符串表示(就是包括浏览器版本信息等的字符串),是识别浏览器的关键,可用于获取当前浏览器版本信息,判断浏览器的类型。
<script> document.write(navigator.userAgent); </script>实例:简单的判断浏览器类型
<script> document.write("操作平台:" + navigator.platform); function userBrowser(){ var name = window.navigator.userAgent; //IE浏览器 //判断IE和非IE可以使用ActiveXObject,只有IE支持该对象 //在IE中window.ActiveXObject返回一个对象,则判断为true //其他浏览器都返回undefine,两个否返回false,进入或判断,也返回false if(!!window.ActiveXObject || "ActiveXObject" in window){ return "IE浏览器"; } //FF浏览器 else if(name.indexOf("Firefox") > -){ return "Firefox浏览器"; } //Chrome浏览器 else if(name.indexOf("Chrome") > -){ return "Chrome浏览器"; } //Opera浏览器 else if(name.indexOf("Opera") > -){ return "Opera浏览器"; } //Safari浏览器 else if(name.indexOf("Safari") > -){ return "Safari浏览器"; } else{ return "unknow"; } } alert("浏览器类型为:" + userBrowser()); </script>5、页面地址。<script> //当前页面URL。若中间出现乱码百分号是中文转码后的显示。 document.write(window.location); </script> <input type="button" value="点击查看" onclick="window.location="http://www.baidu.com","_blank""> </body>location对象其他应用:
<input type="button" value="点击加载" onclick="newDoc()"> <script> function newDoc(){ window.location.assign("http://www.baidu.com") } </script>实例:重新载入当前文档 <input type="button" value="点击载入" onclick="reloadPage()"> <script> function reloadPage(){ location.reload() } </script>实例:用新的文档替换当前文档 <input type="button" value="点击替换" onclick="replaceDoc()"> <script> function replaceDoc(){ window.location.replace("http://www.baidu.com") } </script><script> document.write( "屏幕宽度:" + screen.width + "px" ); document.write("<br>"); document.write( "屏幕高度:" + screen.height + "px" ); </script>(2)、可用宽度和高度
<script> document.write("可用宽度:" + screen.availWidth +"px"); document.write("<br>"); document.write("可用高度:" + screen.availHeight +"px"); </script>
var w = document.documentElement.clientWidth || document.body.clientWidth; var h = document.documentElement.clientHeight || document.body.clientHeight; <script> //对于IE+、Chrome、Firefox、Opera 以及 Safari: document.write("可视宽为:"+window.innerWidth + "<br>"); document.write("可视高为:"+window.innerHeight + "<br>"+ "<br>"); document.write("可视宽为:"+document.documentElement.clientWidth + "<br>"); document.write("可视高为:"+document.documentElement.clientHeight + "<br>"+ "<br>"); //注意该方法返回的数值与其他不同 document.write("可视宽为:"+document.body.clientWidth + "<br>"); document.write("可视高为:"+document.body.clientHeight + "<br>"+ "<br>"); var w= document.documentElement.clientWidth || document.body.clientWidth; var h= document.documentElement.clientHeight || document.body.clientHeight; document.write("可视宽为:"+ w +"<br>"+ "可视高为:" +h); </script>(2)、实际网页尺寸。
var w =document.documentElement.scrollWidth || document.body.scrollWidth; var h =document.documentElement.scrollHeight || document.body.scrollHeight;
<script> //兼容性写法 var w =document.documentElement.scrollWidth || document.body.scrollWidth; var h=document.documentElement.scrollHeight || document.body.scrollHeight; document.write("网页内容宽为:"+ w +"<br>"+ "网页内容高为:" +h); </script>(3)、包含滚动条的网页尺寸。
<script> //兼容性写法 var w = document.documentElement.offsetWidth || document.body.offsetWidth; var h = document.documentElement.offsetHeight || document.body.offsetHeight; document.write("网页内容宽为:"+ w +"<br>"+ "网页内容高为:" +h); </script>
<!DOCTYPE html> <html> <head> <meta charset="UTF-"> <title>JavaScript实例</title> <script> //几款浏览器每次滚动,距离顶部的距离都不一样: //在Chrome为:-----。都是整数递进。 //而在FF下则为:----。每次递进。 //而在IE下则为:----。每次递进则为。 window.onload=function (){ document.onclick=function (){ var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; alert(scrollTop); }; }; </script> </head> <body style="height:px"> </body> </html> 该知识点相当重要,而且内容比较混乱,不容易理解,需要做大量练习,巩固所学。<script> var oD=new Date(); //从当天起天也就是周后是几号 oD.setDate(oD.getDate()+); //设置过期时间周。可在查看cookie中看到user的过期时间为周后 document.cookie="user=小白;expires="+oD; //浏览器关闭删除本条cookie。pass的过期时间为在会话结束时。 document.cookie="pass="; //读取cookie alert(document.cookie); </script>(2)、修改cookie
<script> var oD=new Date(); oD.setDate(oD.getDate()+); document.cookie="user=小白;expires="+oD; document.cookie="pass="; //可在查看cookie中看到user和pass的内容都被新的cookie替换了 document.cookie="user=小明;expires="+oD; document.cookie="pass="; //读取cookie alert(document.cookie); </script>(3)、删除cookie
<!DOCTYPE html> <html> <head> <meta charset="UTF-"> <title>Cookie封装函数</title> </head> <body> <script> //设置cookie //参数:cookie的名字,参数:cookie的值,参数:cookie过期时间 function setCookie(name, value, iDay){var oDate=new Date();oDate.setDate(oDate.getDate()+iDay);document.cookie=name+"="+value+"; expires="+oDate; } //userName一年后过期,passWord两周后过期。 setCookie("userName", "小白", ); setCookie("passWord", "", ); //获取cookie //参数为cookie的名称 function getCookie(name){ //用字符串将cookie分割,注意要用:默认的cookie样式,分号加空格。 var arr=document.cookie.split("; "); for(var i=;i<arr.length;i++){ //默认字符串显示为:a=; b=; c= //所以用等号再做一次分隔 var result=arr[i].split("="); //result的第一位存名称 //如果第一位等于name,就说明找到想要的了,就返回第二位的值。 if(result[]==name){ //result的第二位存值 return result[]; } } //如果没有cookie就返回一个空字符串。比如有些用户是第一次进入网站,就没有产生cookie。 return ""; } //获取cookie中passWord的值 alert(getCookie("passWord")); //参数为删除哪条cookie function removeCookie(name){ //参数:cookie的值,表示随便一个。参数:昨天过期,立马删除。 setCookie(name, , -); } //将cookie中userName和passWord删除。 //在点击查看页面信息,之前保存的cookie就不存在了。 removeCookie("userName"); removeCookie("passWord"); </script> </body> </html> (4)、cookie小应用:记住上一次的用户名<!DOCTYPE html> <html> <head> <meta charset="UTF-"> <title>记住上一次的用户名</title> <script> function setCookie(name, value, iDay){ var oDate=new Date(); oDate.setDate(oDate.getDate()+iDay); document.cookie=name+"="+value+"; expires="+oDate; } function getCookie(name){ var arr=document.cookie.split("; "); for(var i=;i<arr.length;i++){ var result=arr[i].split("="); if(result[]==name){ return result[]; } } return ""; } function removeCookie(name){ setCookie(name, , -); } window.onload=function (){ var oForm=document.getElementById("form"); var oUser=document.getElementsByName("user")[]; //在点击提交按钮时发生的事件 oForm.onsubmit=function (){ //创建一个cookie,值为用户名输入的值,天后过期 setCookie("user", oUser.value, ); }; //用户名的值为获取cookie中的user oUser.value=getCookie("user"); }; </script> </head> <body> //index.html为本文档文件名 <form id="form" action="index.html"> 用户名:<input type="text" name="user"><br> 密 码:<input type="password" name="pass"><br> <input type="submit" name="" value="登录"> </form> </body> </html> window的六大属性,同时它们也是对象: