JavaScript Cookies

 

相关:jquery-cookie

 

cookie 是存储于访问者的计算机中的变量,常用来存储用户名字,密码,日期.

示例:

1 document.cookie="username=John Doe";
2 document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";
3 document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

w3schools Example

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script>
 5 
 6 function setCookie(cname,cvalue,exdays) {
 7     var d = new Date();
 8     d.setTime(d.getTime() + (exdays*24*60*60*1000));
 9     var expires = "expires=" + d.toGMTString();
10     document.cookie = cname+"="+cvalue+"; "+expires;
11 }
12 
13 function getCookie(cname) {
14     var name = cname + "=";
15     var ca = document.cookie.split(';');
16     for(var i=0; i<ca.length; i++) {
17         var c = ca[i];
18         while (c.charAt(0)==' ') c = c.substring(1);
19         if (c.indexOf(name) == 0) {
20             return c.substring(name.length, c.length);
21         }
22     }
23     return "";
24 }
25 
26 function checkCookie() {
27     var user=getCookie("username");
28     if (user != "") {
29         alert("Welcome again " + user);
30     } else {
31        user = prompt("Please enter your name:","");
32        if (user != "" && user != null) {
33            setCookie("username", user, 30);
34        }
35     }
36 }
37 
38 </script>
39 </head>
40 <body onload="checkCookie()">
41 </body>
42 </html>

w3school 实例

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function getCookie(c_name)
 5 {
 6 if (document.cookie.length>0)
 7   {
 8   c_start=document.cookie.indexOf(c_name + "=")
 9   if (c_start!=-1)
10     { 
11     c_start=c_start + c_name.length+1 
12     c_end=document.cookie.indexOf(";",c_start)
13     if (c_end==-1) c_end=document.cookie.length
14     return unescape(document.cookie.substring(c_start,c_end))
15     } 
16   }
17 return ""
18 }
19 
20 function setCookie(c_name,value,expiredays)
21 {
22 var exdate=new Date()
23 exdate.setDate(exdate.getDate()+expiredays)
24 document.cookie=c_name+ "=" +escape(value)+
25 ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
26 }
27 
28 function checkCookie()
29 {
30 username=getCookie('username')
31 if (username!=null && username!="")
32   {alert('Welcome again '+username+'!')}
33 else 
34   {
35   username=prompt('Please enter your name:',"")
36   if (username!=null && username!="")
37     {
38     setCookie('username',username,365)
39     }
40   }
41 }
42 </script>
43 </head>
44 
45 <body onLoad="checkCookie()">
46 </body>
47 </html>

stackoverflow 实例

 1 function createCookie(name, value, days) {
 2     var expires;
 3 
 4     if (days) {
 5         var date = new Date();
 6         date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
 7         expires = "; expires=" + date.toGMTString();
 8     } else {
 9         expires = "";
10     }
11     document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
12 }
13 
14 function readCookie(name) {
15     var nameEQ = encodeURIComponent(name) + "=";
16     var ca = document.cookie.split(';');
17     for (var i = 0; i < ca.length; i++) {
18         var c = ca[i];
19         while (c.charAt(0) === ' ') c = c.substring(1, c.length);
20         if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
21     }
22     return null;
23 }
24 
25 function eraseCookie(name) {
26     createCookie(name, "", -1);
27 }

 

posted @ 2015-12-18 18:15  提佰萬  阅读(213)  评论(0编辑  收藏  举报