`
wbj0110
  • 浏览: 1550954 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

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

    博客分类:
  • Js
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

分享到:
评论

相关推荐

    jquery.cookie.js使用指南

    jquery.cookie.js是一个轻量级的cookie插件,可以读取、写入、删除cookie。 jquery.cookie.js的配置 首先包含jQuery的库文件,在后面包含jquery.cookie.js的库文件。 代码如下: [removed][removed] [removed]...

    jQuery权威指南-源代码

    虽然jQuery使用简单,但它毕竟是一门新的技术,与传统的JavaScript在性能与语法上存在诸多差异,需要相应的书籍来引导开发者们迅速而有效地掌握它,并能真正付诸实践。综观现在已经出版的中文类jQuery图书,不是...

    JQuery权威指南源代码

    使用JavaScript实现隔行变色 使用jQuery选择器实现隔行变色 JavaScript代码检测页面元素 jQuery代码检测页面元素 使用jQuery基本选择器 使用jQuery层次选择器 使用jQuery基本过滤选择器 使用jQuery内容过滤...

    jquery.cookiesplease.js:这是一个非常简单的 jquery 插件,旨在帮助网站应对欧盟关于 cookie 的规则

    这个 cookie 可以被其他脚本使用(结合 jquery.cookie,这个插件的一个依赖项)来确定他们是否可以/应该使用 cookie。 它甚至可以用来确定是否可以加载其他脚本。入门下载或。为什么是另一个 cookie 插件? 已经有...

    jQuery权威指南366页完整版pdf和源码打包

    jQuery权威指南 完整版 pdf 和源码打包!如果觉得好,请删除本资源并购买原版。学习,勤为功。资料,藏为废。书是用来看的,不是用来收藏的。前 言 第1章 jquery开发入门/1 1.1 jquery概述/2 1.1.1 认识...

    JavaScript权威指南(第6版)

    《JavaScript权威指南(第6版)》主要讲述的内容涵盖JavaScript语言本身,以及Web浏览器所实现的JavaScript API。本书第6版涵盖了HTML5和ECMAScript 5,很多章节完全重写,增加了当今Web开发的最佳实践的内容,新增...

    JavaScript权威指南(第6版)中文文字版

    《JavaScript权威指南(第6版)》要讲述的内容涵盖JavaScript语言本身,以及web浏览器所实现的JavaScript API。本书第6版涵盖了 html5 和 ecmascript 5,很多章节完全重写,增加了当今 web 开发的最佳实践的内容,新增...

    JavaScript权威指南(第6版)(中文版)

    《JavaScript权威指南(第6版)》主要讲述的内容涵盖JavaScript语言本身,以及Web浏览器所实现的JavaScript API。本书第6版涵盖了HTML5和ECMAScript 5,很多章节完全重写,增加了当今Web开发的最佳实践的内容,新增...

    JavaScript权威指南(第六版) 清晰-完整

    OReilly精品图书系列:JavaScript权威指南(第6版) 作者简介  David Flanagan是一名程序员,也是一名作家,它的个人网站是 。他在O’Reilly出版的其他畅销书还包括《JavaScript Pocket Reference》、《The Ruby ...

    JavaScript权威指南(第6版)

    《JavaScript权威指南(第6版)》要讲述的内容涵盖JavaScript语言本身,以及web浏览器所实现的JavaScript API。本书第6版涵盖了 html5 和 ecmascript 5,很多章节完全重写,增加了当今 web 开发的最佳实践的内容,新增...

    JavaScript权威指南(第6版) 中文版

    《JavaScript权威指南(第6版)》要讲述的内容涵盖JavaScript语言本身,以及web浏览器所实现的JavaScript API。本书第6版涵盖了 html5 和 ecmascript 5,很多章节完全重写,增加了当今 web 开发的最佳实践的内容,新增...

    python入门到高级全栈工程师培训 第3期 附课件代码

    06 COOKIE和SESSION配合使用 第54章 01 今日内容概要 02 Django内容回顾 03 Django请求生命周期之Http请求 04 Django请求生命周期之FBV和CBV 05 Django请求生命周期之CBV扩展 06 瞎扯淡 07 Django请求生命周期之...

Global site tag (gtag.js) - Google Analytics