window对象
bom(浏览器对象模型)
所有的浏览器都有五个对象
window:窗口对象
location:定位信息(地址栏)
history:历史
Screen:包含有关客户端显示屏幕的信息(例:显示屏幕的宽高)
Navigator:包含有关浏览器的信息(例:版本、名称)
window对象详解:
如果文档包含框架(frame或iframe标签),浏览器会为html文档创建一个window对象,并为每个框架创建一个额外的window对象。
常用的属性:
通过window可以获取其他四个对象
window.location == location
window.history == history
常用的方法:
消息框:
alert(“....”):警告框
confirm(“你确定要删除嘛?”):确定框 返回值:boolean
prompt(“请输入您的姓名”):输入框 返回值:你输入的内容
<html> <head> <meta charset="UTF-8"> <title></title> <script> //警告框 alert("警告"); //确认框 var flag = confirm("are you sure?"); alert(flag); //输入框 var res = prompt("please input your name"); alert(res); </script> </head> <body> </body> </html>
定时器 :
设置定时器
setInterval(code,毫秒数):周期执行
setTimeout(code,毫秒数):延迟多长事件后,只执行一次
例:
setInterval(showad,4000)
setInterval("showad()",4000)
清楚定时器
clearInterval(id):
clearTimeout(id):
打开和关闭:
open(url):打开
close(url):关闭
<html> <body> <input type="button" value="open" onclick="openA()" /> </body> <script> function openA(){ //打开A页面 open("a.html"); } </script> </html>
<html> <head> <meta charset="UTF-8"> <title></title> </head> <body> 我是A <input type="button" name="" id="" value="close1" onclick="close1()"/> </body> </html> <script> function close1(){ close(); } </script>
Location:定位信息
常用属性:
href:获取或设置当前页面的url(定位信息)
location.href;获取url
location.hrel="..."设置url 相当于a标签
<html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="button" value="设置url" onclick="setUrl()" /> </body> <script> //获取url var x = window.location.href alert(x); //设置url function setUrl(){ location.href = "../window/a.html"; } </script> </html>
history:历史
back():后退
forward():向前
go(int)
go(-1) == back()
go(1) == forward()
a页面 <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> a页面 <a href="b.html">b.html</a> <input type="button" value="前进" onclick="forw()" /> </body> <script> function forw(){ history.go(1); } </script> </html> b页面 <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> b页面 <a href="b.html">b.html</a> <input type="button" value="后退" onclick="backt()" /> </body> <script> function backt(){ window.history.go(-1); } </script> </html>

浙公网安备 33010602011771号