码迷,mamicode.com
首页 > 编程语言 > 详细

javascript cookie 操作

时间:2015-09-10 00:20:48      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 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>

 

javascript cookie 操作

标签:

原文地址:http://www.cnblogs.com/cloudshadow/p/javascript_cookie.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!