目的:
1.访问fm.html时,假如没访问过,跳转到fm1.html,访问1次后,跳转到fm2.html,访问2次后,跳转到fm3.html,再次访问跳fm1.html,依次循环
原理:
通过cookie保存访问记录:没访问过时,保存cookie1,访问1次后,保存cookie2,访问2次后,保存cookie3,再次访问fm.html,清除cookie
关键代码:
1.保存cookie
function setCookie(name,value) { var Days = 365; var exp = new Date; exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + ("=" + (value) + ";expires=" + exp.toGMTString() + ";path=/;");}
2.读取cookie
function getCookie(name){ var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) { return (arr[2]); } return null; }
3.删除cookie
function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间 var date = new Date(); date.setTime(date.getTime() - 10000); document.cookie = name + "=a; expires=" + date.toGMTString(); }
另一种方法删除cookie
setcookie("cookiename","")设置某个cookiename值为空
4.跳转代码
var url=window.location.href; var history=getCookie("his"); if(history==""||history==null){ setCookie("his","http://127.0.0.1/fm1.html"); window.location.href="fm1.html" }else{ var index=history.lastIndexOf("/"); var cururl=history.substring(index+3,index+4); var cururll=parseInt(cururl); switch(cururll){ case 1: setCookie("his","http://127.0.0.1/fm2.html"); window.location.href="fm2.html"; break; case 2: setCookie("his","http://127.0.0.1/fm3.html"); window.location.href="fm3.html"; break; default: delCookie("his"); window.location.href="fm.html" } }