WEB前端开发群:17784629
<html> <head> <script type="text/javascript"> function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)) } } return "" } function setCookie(c_name,value,expiredays) { var exdate=new Date() exdate.setDate(exdate.getDate()+expiredays) document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) } function checkCookie() { username=getCookie('username') if (username!=null && username!="") { alert('Welcome again' + username +'!')} else { username=prompt('Please enter your name:',"") if(username!=null && username!="") { setCookie('username',username,365) } else{ alert("O! My GOD!") } } } </script> </head> <body onLoad="checkCookie()"> </body> </html>
提示:你可以先修改部分代码再运行。
Posted in JS基础练习题.
Tagged with Cookies.
No comments
By yflintao – 2010年07月23日
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
留下评论 Cancel Some HTML is OK
Name (required)
Email (required, but never shared)
Web
or, reply to this post via trackback.
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.