js基于cookie记录来宾姓名的方法
本文实例讲述了js基于cookie记录来宾姓名的方法。分享给大家供大家参考,具体如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>无标题文档</title> <scriptlanguage="JavaScript"> <!--Begin varexpDays=30; varexp=newDate(); exp.setTime(exp.getTime()+(expDays*24*60*60*1000)); functionWho(info){ varVisitorName=GetCookie('VisitorName') if(VisitorName==null){ VisitorName=prompt("您的尊姓大名:"); SetCookie('VisitorName',VisitorName,exp); } returnVisitorName; } functionWhen(info){ varrightNow=newDate() varWWHTime=0; WWHTime=GetCookie('WWhenH') WWHTime=WWHTime*1 varlastHereFormatting=newDate(WWHTime); varintLastVisit=(lastHereFormatting.getYear()*10000)+(lastHereFormatting.getMonth()*100)+lastHereFormatting.getDate() varlastHereInDateFormat=""+lastHereFormatting; vardayOfWeek=lastHereInDateFormat.substring(0,3) vardateMonth=lastHereInDateFormat.substring(4,11) vartimeOfDay=lastHereInDateFormat.substring(11,16) varyear=lastHereInDateFormat.substring(23,25) varWWHText=dayOfWeek+","+dateMonth+"at"+timeOfDay SetCookie("WWhenH",rightNow.getTime(),exp) returnWWHText } functionCount(info){ varWWHCount=GetCookie('WWHCount') if(WWHCount==null){ WWHCount=0; } else{ WWHCount++; } SetCookie('WWHCount',WWHCount,exp); returnWWHCount; } functionset(){ VisitorName=prompt("Whoareyou?"); SetCookie('VisitorName',VisitorName,exp); SetCookie('WWHCount',0,exp); SetCookie('WWhenH',0,exp); } functiongetCookieVal(offset){ varendstr=document.cookie.indexOf(";",offset); if(endstr==-1) endstr=document.cookie.length; returnunescape(document.cookie.substring(offset,endstr)); } functionGetCookie(name){ vararg=name+"="; varalen=arg.length; varclen=document.cookie.length; vari=0; while(i<clen){ varj=i+alen; if(document.cookie.substring(i,j)==arg) returngetCookieVal(j); i=document.cookie.indexOf("",i)+1; if(i==0)break; } returnnull; } functionSetCookie(name,value){ varargv=SetCookie.arguments; varargc=SetCookie.arguments.length; varexpires=(argc>2)?argv[2]:null; varpath=(argc>3)?argv[3]:null; vardomain=(argc>4)?argv[4]:null; varsecure=(argc>5)?argv[5]:false; document.cookie=name+"="+escape(value)+ ((expires==null)?"":(";expires="+expires.toGMTString()))+ ((path==null)?"":(";path="+path))+ ((domain==null)?"":(";domain="+domain))+ ((secure==true)?";secure":""); } functionDeleteCookie(name){ varexp=newDate(); exp.setTime(exp.getTime()-1); varcval=GetCookie(name); document.cookie=name+"="+cval+";expires="+exp.toGMTString(); } //End--> </script> </head> <body> 提示:在第一次来到本页时提醒你输入你的姓名,以后就不用了,它会每次在你来时告诉你是第多少次来,并且注明上次来本页的时间<br/> <scriptlanguage="JavaScript"> if(Count()==0) { document.write("您好!"+Who()+".您首次来这儿,请多提宝贵意见."); } else { document.write("您好!"+Who()+".您已来这儿"+Count()+"次了,最后一次在"+When()+"."); } </script> </body> </html>
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。