1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>Javascript cookie</title> 5 <script type="text/javascript"> 6 function getCookie(c_name) 7 { 8 alert(document.cookie); 9 if (document.cookie.length>0) 10 { 11 c_start=document.cookie.indexOf(c_name + "="); 12 if (c_start!=-1) 13 { 14 c_start=c_start + c_name.length+1; 15 c_end=document.cookie.indexOf(";",c_start); 16 if (c_end==-1) c_end=document.cookie.length 17 return unescape(document.cookie.substring(c_start,c_end)); 18 } 19 } 20 return ""; 21 } 22 23 function setCookie(c_name,value,expiredays) 24 { 25 var exdate=new Date(); 26 exdate.setDate(exdate.getDate() + expiredays); 27 document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); 28 } 29 30 function delCookie(name) 31 { 32 var exp = new Date(); 33 exp.setTime(exp.getTime() - 1); 34 var cval = getCookie(name); 35 if (cval != null) 36 { 37 document.cookie= name + "=" + cval + ";expires=" + exp.toGMTString(); 38 } 39 } 40 41 function checkCookie() 42 { 43 var username = getCookie(‘testJavascriptCookie‘); 44 if (username != null && username != "") 45 { 46 alert(‘Welcome again ‘ + username + ‘!‘); 47 } 48 else 49 { 50 username = prompt(‘Please enter your name:‘, ""); 51 if (username != null && username != "") 52 { 53 setCookie(‘testJavascriptCookie‘, username, 365); 54 } 55 } 56 } 57 58 </script> 59 </head> 60 61 <body onLoad="checkCookie();"> 62 </body> 63 </html>
时间: 2024-10-12 04:45:28