jquery操作cookie

本文详细介绍了JavaScript Cookie插件的使用方法,包括如何创建、获取、设置和删除Cookie,以及如何通过参数调整Cookie的过期时间、路径、域名和安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[javascript]  view plain copy
  1. /** 
  2.  * Cookie plugin 
  3.  * 
  4.  * Copyright (c) 2006 Klaus Hartl (stilbuero.de) 
  5.  * Dual licensed under the MIT and GPL licenses: 
  6.  * http://www.opensource.org/licenses/mit-license.php 
  7.  * http://www.gnu.org/licenses/gpl.html 
  8.  * 
  9.  */  
  10.   
  11. /** 
  12.  * Create a cookie with the given name and value and other optional parameters. 
  13.  * 
  14.  * @example $.cookie('the_cookie', 'the_value'); 
  15.  * @desc Set the value of a cookie. 
  16.  * @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true}); 
  17.  * @desc Create a cookie with all available options. 
  18.  * @example $.cookie('the_cookie', 'the_value'); 
  19.  * @desc Create a session cookie. 
  20.  * @example $.cookie('the_cookie', null); 
  21.  * @desc Delete a cookie by passing null as value. 
  22.  * 
  23.  * @param String name The name of the cookie. 
  24.  * @param String value The value of the cookie. 
  25.  * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. 
  26.  * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. 
  27.  *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted. 
  28.  *                             If set to null or omitted, the cookie will be a session cookie and will not be retained 
  29.  *                             when the the browser exits. 
  30.  * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). 
  31.  * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). 
  32.  * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will 
  33.  *                        require a secure protocol (like HTTPS). 
  34.  * @type undefined 
  35.  * 
  36.  * @name $.cookie 
  37.  * @cat Plugins/Cookie 
  38.  * @author Klaus Hartl/klaus.hartl@stilbuero.de 
  39.  */  
  40.   
  41. /** 
  42.  * Get the value of a cookie with the given name. 
  43.  * 
  44.  * @example $.cookie('the_cookie'); 
  45.  * @desc Get the value of a cookie. 
  46.  * 
  47.  * @param String name The name of the cookie. 
  48.  * @return The value of the cookie. 
  49.  * @type String 
  50.  * 
  51.  * @name $.cookie 
  52.  * @cat Plugins/Cookie 
  53.  * @author Klaus Hartl/klaus.hartl@stilbuero.de 
  54.  */  
  55. jQuery.cookie = function(name, value, options) {  
  56.     if (typeof value != 'undefined') { // name and value given, set cookie  
  57.         options = options || {};  
  58.         if (value === null) {  
  59.             value = '';  
  60.             options.expires = -1;  
  61.         }  
  62.         var expires = '';  
  63.         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  
  64.             var date;  
  65.             if (typeof options.expires == 'number') {  
  66.                 date = new Date();  
  67.                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  
  68.             } else {  
  69.                 date = options.expires;  
  70.             }  
  71.             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE  
  72.         }  
  73.         var path = options.path ? '; path=' + options.path : '';  
  74.         var domain = options.domain ? '; domain=' + options.domain : '';  
  75.         var secure = options.secure ? '; secure' : '';  
  76.         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  
  77.     } else { // only name given, get cookie  
  78.         var cookieValue = null;  
  79.         if (document.cookie && document.cookie != '') {  
  80.             var cookies = document.cookie.split(';');  
  81.             for (var i = 0; i < cookies.length; i++) {  
  82.                 var cookie = jQuery.trim(cookies[i]);  
  83.                 // Does this cookie string begin with the name we want?  
  84.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  
  85.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
  86.                     break;  
  87.                 }  
  88.             }  
  89.         }  
  90.         return cookieValue;  
  91.     }  
  92. };  


 

提供方便方法操作cookie : 
$.cookie('the_cookie'); // 获得cookie
$.cookie('the_cookie', 'the_value'); // 设置cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie  7天
$.cookie('the_cookie', '', { expires: -1 }); // 删除
$.cookie('the_cookie', null); // 删除 cookie

设置cookie的名值对,有效期,路径,域,安全
$.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});

 

如果在action中出现乱码,需URLDecoder.decode(cookievalue,"utf-8")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值