【jquery】jquery.cookie.js 的使用指南

jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 cookie。本文主要针对 jquery.cookie.js 的用法进行详细的介绍。


使用方法:


设置 cookie:

  1. $.cookie('the_cookie', 'the_value');
复制代码



注:如果 $.cookie 没有第三个参数,那么当浏览器关闭时,该 cookie 将会自动删除。


设置一个有效期为 7 天的 cookie:

  1. $.cookie('the_cookie', 'the_value', {expires: 7});
复制代码

注:$.cookie 第三个参数是一个对象,除了可以设置有效期(expires: 7),还可以设置有效路径(path: '/')、有效域(domain: 'jquery.com')及安全性(secure: true)。


读取 cookie:

  1. $.cookie('the_cookie');
复制代码

注:如果没有该 cookie,返回 null。


删除 cookie:

  1. $.cookie('the_cookie', null);
复制代码

我们只需要给需要删除的 cookie 设置为 null,就可以删除该 cookie。


最后附上源代码:

  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. * Create a cookie with the given name and value and other optional parameters.
  12. *
  13. * @example $.cookie('the_cookie', 'the_value');
  14. * @desc Set the value of a cookie.
  15. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
  16. * @desc Create a cookie with all available options.
  17. * @example $.cookie('the_cookie', 'the_value');
  18. * @desc Create a session cookie.
  19. * @example $.cookie('the_cookie', null);
  20. * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
  21. *       used when the cookie was set.
  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. * Get the value of a cookie with the given name.
  42. *
  43. * @example $.cookie('the_cookie');
  44. * @desc Get the value of a cookie.
  45. *
  46. * @param String name The name of the cookie.
  47. * @return The value of the cookie.
  48. * @type String
  49. *
  50. * @name $.cookie
  51. * @cat Plugins/Cookie
  52. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  53. */
  54. jQuery.cookie = function(name, value, options) {
  55.     if (typeof value != 'undefined') { // name and value given, set cookie
  56.         options = options || {};
  57.         if (value === null) {
  58.             value = '';
  59.             options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
  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.         // NOTE Needed to parenthesize options.path and options.domain
  74.         // in the following expressions, otherwise they evaluate to undefined
  75.         // in the packed version for some reason...
  76.         var path = options.path ? '; path=' + (options.path) : '';
  77.         var domain = options.domain ? '; domain=' + (options.domain) : '';
  78.         var secure = options.secure ? '; secure' : '';
  79.         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  80.     } else { // only name given, get cookie
  81.         var cookieValue = null;
  82.         if (document.cookie && document.cookie != '') {
  83.             var cookies = document.cookie.split(';');
  84.             for (var i = 0; i < cookies.length; i++) {
  85.                 var cookie = jQuery.trim(cookies[i]);
  86.                 // Does this cookie string begin with the name we want?
  87.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
  88.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  89.                     break;
  90.                 }
  91.             }
  92.         }
  93.         return cookieValue;
  94.     }
  95. };
复制代码



via:http://www.cnblogs.com/yjzhu/archive/2015/03/23/4359420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值