前言
本章学习JS的window对象的相关知识
方法
特别的,window对象不用new,直接进行使用即可,类似Math的使用方式,window关键字可以省略不写。
一、框体方法
alert(内容)
该方法提示一个警告信息,没有返回,这个我们已经用了多次了,这次终于知道怎么回事了!
例子我也不多说了。
confirm(内容)
该方法是一个确认框,提示用户选择一项操作(确定/取消)
范例:模拟提交
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS事件</title>
<script type="text/javascript">
function change(){
if(confirm("确认提交?")){
alert("提交成功!");
}
}
</script>
</head>
<body>
用户名:<input type="text" id="test" value="" /><br>
密 码:<input type="password" name="" /><br>
<input type="button" name="" id="" value="提交" onclick="change();"/>
</body>
</html>
prompt(提示内容,默认值)
提示框, 提示用某个信息的录入
点击确定,返回当前录入的数据,默认返回空字符串
点击取消,返回null
范例:模拟提示框
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS事件</title>
<script type="text/javascript">
function change(){
var str = prompt("请输入短信验证码:","请输入短信验证码");
if(str == '123'){
alert("验证成功!");
}else{
alert("验证失败!");
}
}
</script>
</head>
<body>
用户名:<input type="text" id="test" value="" /><br>
密 码:<input type="password" name="" /><br>
<input type="button" name="" id="" value="提交" onclick="change();"/>
</body>
</html>
二、定时和间隔方法
setTimeout:指定的时间后执行指定的函数
参数1:函数对象
参数2:时间,单位毫秒。
返回值:返回当前定时器的id
范例:两秒后执行某个函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS事件</title>
<script type="text/javascript">
function change(){
setTimeout(function(){
alert("提交成功!");
},2000);
}
</script>
</head>
<body>
用户名:<input type="text" id="test" value="" /><br>
密 码:<input type="password" name="" /><br>
<input type="button" name="" id="" value="提交" onclick="change();"/>
</body>
</html>
setInterval:每间隔指定的时间执行指定的函数
参数1:函数对象
参数2:时间,单位毫秒。
返回值:返回当前间隔器的id
范例:每隔两秒执行相应函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS事件</title>
<script type="text/javascript">
setInterval(function(){
alert("我每两秒执行一次!");
},2000);
</script>
</head>
<body>
用户名:<input type="text" id="test" value="张三" /><br>
密 码:<input type="password" name="" value="123"/><br>
<input type="button" name="" id="" value="提交"/>
</body>
</html>
clearTimeout:用来停止指定的定时器
参数:定时器的id
clearInterval:用来停止指定的间隔器
参数:间隔器的id
三、子窗口方法
window.open('子页面的资源(相对路径)','打开方式','配置');
示例:在一个窗口打开另一个窗口
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS事件</title>
<script type="text/javascript">
function change(){
window.open("03_WJ_form.html");
}
</script>
</head>
<body>
用户名:<input type="text" id="test" value="" /><br>
密 码:<input type="password" name="" /><br>
<input type="button" name="" id="" value="提交" onclick="change();"/>
</body>
</html>
window.close();关闭页面
window.opener.父页面的函数 该对象可以调用父页面相关函数
四、window对象的常用属性
地址栏属性:location
window.location.href="新的资源路径(相对路径/URL)"
window.location.reload()重新加载页面资源
范例:点击提交将网页定位到其他网页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS事件</title>
<script type="text/javascript">
function change(){
window.location.href="03_WJ_form.html";
}
</script>
</head>
<body>
用户名:<input type="text" id="test" value="" /><br>
密 码:<input type="password" name="" /><br>
<input type="button" name="" id="" value="提交" onclick="change();"/>
</body>
</html>
特别的:注意与window.open的区别,open打开的新窗口,而location覆盖本页面打开新网页
历史记录属性
window.history.forward() 页面资源前进,历史记录的前进。
window.history.back() 页面资源后退,历史记录后退
window.history.go(index) 跳转到指定的历史记录资源
注意window.history.go(0)相当于刷新。
屏幕属性
window.srceen.width;//获取屏幕的宽度分辨率
window.screen.height;//获取屏幕的高度分辨率