javascript之dom编程(二):常用对象1

javascript之dom编程(2):常用对象1
一:history对象
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script type="text/javascript">
//history对象 记录用户访问的url
function myback(){
window.alert(history.length);
window.history.go(-1);
}
</script>
<body>
<h1>b.html</h1>
<input type="button" onClick="myback()" value="返回上次页面1"/>
</body>

</html>


二:location对象
<script type="text/javascript">
//location对象包含当前url的信息
function myfresh(){
window.location.reload();
}
//每隔十秒刷新页面
window.setInterval("myfresh()",10000);

</script>


三:navigator对象
<script type="text/javascript">
//navigator该对象包括浏览器的信息,想看该对象下的所有属性可以通过遍历获得。
for(var key in navigator){
document.write("<br>"+key+"="+navigator[key]);
}

</script>


四:event对象
案例一:
<script type="text/javascript">
var i=5;
function countTime(){
i--;
mybut.value="同意  "+i;
if(i==0){
mybut.disabled=false;
window.clearInterval(mytimer);
}
}
var mytimer=window.setInterval("countTime


()",1000);
</script>
<body>
<P>
注册条款
</p>
<input type="button" id="mybut" disabled="true" value="同意  5">
</body>
案例二:检测鼠标移动
<script type="text/javascript">
function test(){
showxy.innerText="x="+window.event.screenX+"y="+window.event.screenY;
}
</script>
</head>
<body>

<div onmousemove="test();" style="width:400px;height:300px;border:1px solid red;">

</div>

<span id="showxy"></span>
</body> 
案例三:
<script type="text/javascript">
//文本框中输入一个六位数,第一位不能为0,不能超过六位数,必须全是数字,
var i=0;
function checkNum(obj){
if(i==6){
window.alert("输入的字符串长度>6");
return false;
}
//首位不能是0
if(i==0){
if(window.event.keyCode=='0'.charCodeAt(0)){
alert('首位不能是0');
return false;
}
}
if(window.event.keyCode<'0'.charCodeAt(0)||window.event.keyCode>'9'.charCodeAt(0)){
alert("你输入的不是数");
return false;
}else{
i++;
}
}
</script>
</head>
<body>
请输入一个六位数 <input type="text" id="pageNow" onkeydown="return  checkNum(this)"/>
</body> 

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21