zoukankan      html  css  js  c++  java
  • js cookie 操作

     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>
  • 相关阅读:
    P3391 文艺平衡树
    隔离村庄(树形dp[01背包])
    cmd指令集
    vs的使用
    博客园第一天
    蓝桥杯 小生物的逃逸 模拟
    蓝桥杯 自行车停放 双向链表
    c++字符数组函数总结
    蓝桥杯 石子游戏 贪心
    蓝桥杯 最大获利 模拟
  • 原文地址:https://www.cnblogs.com/cloudshadow/p/js_cookie.html
Copyright © 2011-2022 走看看