首页 > 代码库 > js设置、获取、清除cookie

js设置、获取、清除cookie

  1 <!DOCTYPE html>  2 <html lang="en">  3  <head>   4     <meta charset="utf-8">  5     <meta name="description" content="">  6     <meta name="HandheldFriendly" content="True">  7     <meta name="MobileOptimized" content="320">  8     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">  9     <title>cookies</title> 10     <!-- html5.js for ie less than 9 --> 11     <!--[if lt ie 9]> 12         <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 13     <![endif]--> 14  15     <!-- css3-mediaqueries.js for ie less than 9 --> 16     <!--[if lt ie 9]> 17         <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script> 18     <![endif]--> 19     <style type="text/css"> 20     body,div,ul,li,a,img,h1,h2,h3,h4,h5,p,input{ 21         margin:0; 22         padding:0;         23     } 24     article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {  25         display: block; 26     } 27     body{ 28         font-family:"微软雅黑",Verdana, Arial, Helvetica, sans-serif;     29         -webkit-text-size-adjust:none; 30     } 31     img,input{ 32         outline:none; 33     } 34     ul{ 35         list-style:none; 36     } 37     a:link{ 38         text-decoration:none; 39         color:#545454; 40     } 41     a:visited{ 42         color:#525252; 43     } 44     a:hover{ 45         color:#4D4D4D; 46     } 47     .clear{ 48       clear:both; 49     } 50     .ui-page {  51         -webkit-backface-visibility: hidden;  52     } 53     </style> 54  </head> 55  <body> 56     <div id="time"></div> 57     <script src="jquery.js"></script> 58     <script type="text/javascript"> 59         (function(){ 60             var time=new Date(); 61             var str; 62             str=time.getSeconds(); 63             $(window).click(function(){ 64                 Cookies.set(istime,str); 65             }) 66             var Cookies ={ 67                 //设置cookies 68                 set:function(name,value,days){ 69                     if (days) { 70                         var date = new Date(); 71                         date.setTime(date.getTime()+(days*24*60*60*1000)); 72                         var expires = "; expires="+date.toGMTString(); 73                     } 74                     else var expires = ""; 75                     document.cookie = name+"="+value+expires+"; path=/"; 76                 }, 77  78                 //获取cookies 79                 get:function(name){ 80                     var nameEQ = name + "="; 81                     var ca = document.cookie.split(;); 82                     for(var i=0;i < ca.length;i++) { 83                         var c = ca[i]; 84                         while (c.charAt(0)== ) c = c.substring(1,c.length); 85                         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 86                     } 87                     return null; 88                 }, 89  90                 //清除cookies 91                 erase:function(name){ 92                     this.set(name,"",-1); 93                 } 94             } 95  96             var isTime = function(){ 97                 var _istime = Cookies.get(istime); //获取name为‘istime‘的cookies; 98                 if(_istime ){  //判断name为‘istime‘的cookies存不存在; 99                         var time1=new Date();100                         var str1;101                         str1=time.getSeconds();102                         103                         if(str1 - _istime > 0 ){104                             alert(str1 - _istime);105                         }106                         107                 }108             };109             110             $(function(){111                 isTime();112             })113         })(this)114         115     </script>116  </body>117 </html>

 

js设置、获取、清除cookie