实例1,js根据日期串获得年龄:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
function getAge(str){
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
alert(r[1]);
alert(r[2]);
alert(r[3]);
alert(r[4]);
alert(r[5]);
var birth= new Date(r[1],r[3]-1,r[4]);
if(birth.getFullYear()==r[1]&&(birth.getMonth()+1)==r[3]&&birth.getDate()==r[4]){
var today = new Date();
var age = today.getFullYear()-r[1];
if(today.getMonth()>birth.getMonth()){
return age;
}
if(today.getMonth()==birth.getMonth()){
if(today.getDate()>=birth.getDate()){
return age;
}else{
return age-1;
}
}
if(today.getMonth()<birth.getMonth()){
return age-1;
}
}
return("输入的日期格式错误!");
}
alert(getAge("1992-10-30"));
</script>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
实例2,防止页面后退的操作:
window.history.forward(1);
实例3,浏览器日志打印
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
function getAge(str){
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
alert(r[1]);
alert(r[2]);
alert(r[3]);
alert(r[4]);
alert(r[5]);
var birth= new Date(r[1],r[3]-1,r[4]);
if(birth.getFullYear()==r[1]&&(birth.getMonth()+1)==r[3]&&birth.getDate()==r[4]){
var today = new Date();
var age = today.getFullYear()-r[1];
if(today.getMonth()>birth.getMonth()){
return age;
}
if(today.getMonth()==birth.getMonth()){
if(today.getDate()>=birth.getDate()){
return age;
}else{
return age-1;
}
}
if(today.getMonth()<birth.getMonth()){
return age-1;
}
}
return("输入的日期格式错误!");
}
alert(getAge("1992-10-30"));
</script>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
实例2,防止页面后退的操作:
window.history.forward(1);
实例3,浏览器日志打印
console.log("控制台打印");跟alert();差不多,只不过是在控制台打印;
实例4:
if(result="ok"){
with(document.getElementById("addUserForm")){
action=url;//下面的属性和方法是上面的with(对象)里的对象的属性和方法;
method="post";
submit();//触发表单提交的方法
}
}