jquery.cookie使用方法

5 篇文章 0 订阅
//使用方法如下:
//设置cookie的名值对
//$.cookie(’name’, ‘value’);
//设置cookie的名值对,有效期,路径,域,安全
//$.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});
//新建一个cookie 包括有效期 路径 域名等
//读取cookie的值
//var account= $.cookie(‘name’);
//删除一个cookie
//example $.cookie(’name’, null);

 

[javascript] view plain copy print ?
  1.  jQuery.cookie = function(name, value, options) {    
  2.   
  3.     if (typeof value != 'undefined') { // name and value given, set cookie     
  4.   
  5.         options = options || {};    
  6.   
  7.         if (value === null) {    
  8.   
  9.             value = '';    
  10.   
  11.             options.expires = -1;    
  12.   
  13.         }    
  14.   
  15.         var expires = '';    
  16.   
  17.         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {    
  18.   
  19.             var date;    
  20.   
  21.             if (typeof options.expires == 'number') {    
  22.   
  23.                 date = new Date();    
  24.   
  25.                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));    
  26.   
  27.             } else {    
  28.   
  29.                 date = options.expires;    
  30.   
  31.             }    
  32.   
  33.             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE     
  34.   
  35.         }    
  36.   
  37.         // CAUTION: Needed to parenthesize options.path and options.domain     
  38.   
  39.         // in the following expressions, otherwise they evaluate to undefined     
  40.   
  41.         // in the packed version for some reason...     
  42.         var path = options.path ? '; path=' + (options.path) : '';    
  43.         var domain = options.domain ? '; domain=' + (options.domain) : '';    
  44.   
  45.         var secure = options.secure ? '; secure' : '';    
  46.   
  47.         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');    
  48.   
  49.     } else { // only name given, get cookie     
  50.   
  51.         var cookieValue = null;    
  52.         if (document.cookie && document.cookie != '') {    
  53.   
  54.             var cookies = document.cookie.split(';');    
  55.   
  56.             for (var i = 0; i < cookies.length; i++) {    
  57.   
  58.                 var cookie = jQuery.trim(cookies[i]);    
  59.   
  60.                 // Does this cookie string begin with the name we want?     
  61.   
  62.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {    
  63.   
  64.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));    
  65.   
  66.                     break;    
  67.   
  68.                 }    
  69.   
  70.             }    
  71.   
  72.         }    
  73.   
  74.         return cookieValue;    
  75.   
  76.     }    
  77.   
  78. };   
  jQuery.cookie = function(name, value, options) {  

     if (typeof value != 'undefined') { // name and value given, set cookie  

         options = options || {};  

         if (value === null) {  

             value = '';  

             options.expires = -1;  

         }  

         var expires = '';  

         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  

             var date;  

             if (typeof options.expires == 'number') {  

                 date = new Date();  

                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  

             } else {  

                 date = options.expires;  

             }  

             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE  

         }  

         // CAUTION: Needed to parenthesize options.path and options.domain  

         // in the following expressions, otherwise they evaluate to undefined  

         // in the packed version for some reason...  
         var path = options.path ? '; path=' + (options.path) : '';  
         var domain = options.domain ? '; domain=' + (options.domain) : '';  

         var secure = options.secure ? '; secure' : '';  

         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  

     } else { // only name given, get cookie  

         var cookieValue = null;  
         if (document.cookie && document.cookie != '') {  

             var cookies = document.cookie.split(';');  

             for (var i = 0; i < cookies.length; i++) {  

                 var cookie = jQuery.trim(cookies[i]);  

                 // Does this cookie string begin with the name we want?  

                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  

                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  

                     break;  

                 }  

             }  

         }  

         return cookieValue;  

     }  

 }; 

这是掌握cookie最后的一个障碍:缺省情况下cookie只能被在同一个Web服务器上同一个路径下设置了该cookie的网页读取.例如,如果在http://kenny.safeter.com/food/kenny/banana_kenny.htm"有一段Javascript询问了用户的姓名,你可能需要在你的另一个网页例如主页中访问一个给定的名字.所以你必须设定该cookie的路径.路径"path"用于设置可以读取一个cookie的最顶层的目录.将cookie的路径设置为你的网页最顶层的目录可以让该该目录下的所有网页都能访问该cookie.

方法:

  在你的cookie中加入path=/; 如果你只想让"food" 目录中的网页可以使用该cookie,则你加入path=/food;.还有一点:有些网站有许多小的域名,例如访问可能还在
"chimp.webmonkey.com," "gorilla.webmonkey.com," 和
"ape.webmonkey.com." 域名下有网页.缺省情况下只有
"chimp.webmonkey.com" 域下的网页可以读取该cookie.如果
你想让"webmonkey.com"下的所有机器都可以读取该cookie,我
们必须在cookie中加入 "domain=webmonkey.com" .

要将一个cookie设置在http://chimp.webmonkey.com/food/bananas/banana_puree.htm ,并且让所有访问的网页都可以利用它,我们可以这样:

 

[javascript] view plain copy print ?
  1. function setCookie()  
  2.   
  3. {  
  4.   
  5.     var the_name = prompt("What's your name?","");  
  6.   
  7.     var the_cookie ="cookie_puss=" + escape(the_name) + ";" ;  
  8.   
  9.     var the_cookie = the_cookie+ "path=/;";  
  10.   
  11.     var the_cookie = the_cookie + "domain=webmonkey.com;";  
  12.   
  13.   
  14.     document.cookie =the_cookie;  
  15.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值