A-A+

一个完美的JavaScript操作COOKIE类

2016年10月14日 前端设计 暂无评论 阅读 6 views 次

文章分享一个非常不错的JavaScript操作COOKIE类,有需要的朋友可参考使用,代码如下:

  1. /**提供客户端cookie操作类 
  2.   
  3.  * 
  4.  * @param string uniqueN 唯一标识 
  5.  * 
  6.  * @author (凹凸曼)lyc 
  7.  * @email jar-c@163.com 
  8.  * 
  9.  */  
  10. var cacheLY = function(uniqueN){  
  11.     var uniqueN = (typeof(uniqueN) != "string") ? "" : "uniqueN_" + uniqueN + "_";  
  12.     setCookie = function(name, value){  
  13.         var Days = 1;  
  14.         var exp = new Date();  
  15.         exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);  
  16.         document.cookie = name + "=" + escape(this.encode(value)) + ";expires=" + exp.toGMTString();  
  17.     }  
  18.    
  19.     getCookie = function(name){  
  20.         var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));  
  21.         if (arr != null)  
  22.             return this.unencode(unescape(arr[2]));  
  23.         return null;  
  24.     }  
  25.     delCookie = function(name){  
  26.         var exp = new Date();  
  27.         exp.setTime(exp.getTime() - 1);  
  28.         var tem = this.getCookie(name);  
  29.         if (tem != null)  
  30.             document.cookie = "name=" + tem + ";expires=" + exp.toGMTString();  
  31.     }  
  32.     encode = function(str){  
  33.         var temstr = "";  
  34.         var i = str.length - 1;  
  35.         for (i; i >= 0; i--) {  
  36.             temstr += str.charCodeAt(i);  
  37.             if (i)  
  38.                 temstr += "a";  
  39.         }  
  40.         return temstr;  
  41.     }  
  42.     unencode = function(str){  
  43.         var strarr = "";  
  44.         var temstr = "";  
  45.         strarr = str.split("a");  
  46.         var i = strarr.length - 1;  
  47.         for (i; i >= 0; i--) {  
  48.             temstr += String.fromCharCode(eval(strarr[i]));  
  49.         }  
  50.         return temstr;  
  51.     }  
  52.     return {  
  53.    
  54.         setValue: function(text){  
  55.    
  56.             setCookie(uniqueN, text);  
  57.         },  
  58.         clearCache: function(name){  
  59.            delCookie(name);  
  60.         },  
  61.         loadCache: function(){  
  62.  //xiariboke.net  
  63.             var temvalue = getCookie(uniqueN);  
  64.             return temvalue;  
  65.         }  
  66.     }  
  67. }  

测试,代码如下:

  1. <title>js控制cookie实现客户端缓存</title>  
  2.         <script src="cache.class.js" type="text/javascript">  
  3.         </script>  
  4.     </head>  
  5.     <body>  
  6.         <div id="nihao">  
  7.         </div>  
  8.         <script type="text/javascript">  
  9.             /* 
  10.              * @param string tem 需要缓存的数据 
  11.              * @param object cache 缓存对象 
  12.              * @param string re  得到的缓存数据 
  13.              * 
  14.              */  
  15.             var tem = '<div id="d"><br><br><br>奥特曼在线</div>';  
  16.             var cache = new cacheLY("123");  
  17.             cache.setValue(tem);  
  18.             var re = cache.loadCache();  
  19.             document.getElementById("nihao").innerHTML = re;  
  20.    
  21.         </script>  
标签:

给我留言