JQuery:cookie插件

http://plugins.jquery.com/cookie/

 1 代码
 2 
 3 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <html> 
 4   <head> 
 5     <title>JQuery-Cookie插件</title> 
 6     <script type="text/javascript" src="jquery-1.4.js"></script> 
 7     <script type="text/javascript" src="jquery.cookie.js"></script> 
 8   </head> 
 9   <body> 
10     <a href="#">设置cookie1</a><br> 
11     <a href="#">设置cookie2</a><br> 
12     <a href="#">获取cookie</a><br> 
13     <a href="#">删除cookie</a><br> 
14   </body> 
15 </html> 
16 <script type="text/javascript"> 
17 $(function(){ 
18    var COOKIE_NAME = 'test_cookie'; 
19    //设置cookie,通过时间间隔 
20    $('a').eq(0).click(function() { 
21        $.cookie(COOKIE_NAME, 'test', { path: '/', expires: 1 }); 
22        return false; 
23    }); 
24    // 设置cookie,到期时间 
25    $('a').eq(1).click(function() { 
26        var date = new Date(); 
27        date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000)); 
28        $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date }); 
29        return false; 
30    }); 
31    // 获取 cookie 
32    $('a').eq(2).click(function() { 
33        alert($.cookie(COOKIE_NAME)); 
34        return false; 
35    }); 
36    // 删除cookie 
37    $('a').eq(3).click(function() { 
38        $.cookie(COOKIE_NAME, null, { path: '/' }); 
39        return false; 
40    }); 
41 }); 
42 </script>
简单使用方法
 1 代码
 2 
 3 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 jQuery.cookie = function(name, value, options) { 
 4     if (typeof value != 'undefined') { // name and value given, set cookie 
 5         options = options || {}; 
 6         if (value === null) { 
 7             value = ''; 
 8             options.expires = -1; 
 9         } 
10         var expires = ''; 
11         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
12             var date; 
13             if (typeof options.expires == 'number') { 
14                 date = new Date(); 
15                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); 
16             } else { 
17                 date = options.expires; 
18             } 
19             expires = '; expires=' + date.toUTCString(); 
20         } 
21         var path = options.path ? '; path=' + (options.path) : ''; 
22         var domain = options.domain ? '; domain=' + (options.domain) : ''; 
23         var secure = options.secure ? '; secure' : ''; 
24         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
25     } else { 
26         var cookieValue = null; 
27         if (document.cookie && document.cookie != '') { 
28             var cookies = document.cookie.split(';'); 
29             for (var i = 0; i < cookies.length; i++) { 
30                 var cookie = jQuery.trim(cookies[i]); 
31                 if (cookie.substring(0, name.length + 1) == (name + '=')) { 
32                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 
33                     break; 
34                 } 
35             } 
36         } 
37         return cookieValue; 
38     } 
39 };
插件的源代码
  1 /*!
  2  * jQuery Cookie Plugin v1.4.1
  3  * https://github.com/carhartl/jquery-cookie
  4  *
  5  * Copyright 2013 Klaus Hartl
  6  * Released under the MIT license
  7  */
  8 (function (factory) {
  9     if (typeof define === 'function' && define.amd) {
 10         // AMD
 11         define(['jquery'], factory);
 12     } else if (typeof exports === 'object') {
 13         // CommonJS
 14         factory(require('jquery'));
 15     } else {
 16         // Browser globals
 17         factory(jQuery);
 18     }
 19 }(function ($) {
 20 
 21     var pluses = /+/g;
 22 
 23     function encode(s) {
 24         return config.raw ? s : encodeURIComponent(s);
 25     }
 26 
 27     function decode(s) {
 28         return config.raw ? s : decodeURIComponent(s);
 29     }
 30 
 31     function stringifyCookieValue(value) {
 32         return encode(config.json ? JSON.stringify(value) : String(value));
 33     }
 34 
 35     function parseCookieValue(s) {
 36         if (s.indexOf('"') === 0) {
 37             // This is a quoted cookie as according to RFC2068, unescape...
 38             s = s.slice(1, -1).replace(/\"/g, '"').replace(/\\/g, '\');
 39         }
 40 
 41         try {
 42             // Replace server-side written pluses with spaces.
 43             // If we can't decode the cookie, ignore it, it's unusable.
 44             // If we can't parse the cookie, ignore it, it's unusable.
 45             s = decodeURIComponent(s.replace(pluses, ' '));
 46             return config.json ? JSON.parse(s) : s;
 47         } catch(e) {}
 48     }
 49 
 50     function read(s, converter) {
 51         var value = config.raw ? s : parseCookieValue(s);
 52         return $.isFunction(converter) ? converter(value) : value;
 53     }
 54 
 55     var config = $.cookie = function (key, value, options) {
 56 
 57         // Write
 58 
 59         if (value !== undefined && !$.isFunction(value)) {
 60             options = $.extend({}, config.defaults, options);
 61 
 62             if (typeof options.expires === 'number') {
 63                 var days = options.expires, t = options.expires = new Date();
 64                 t.setTime(+t + days * 864e+5);
 65             }
 66 
 67             return (document.cookie = [
 68                 encode(key), '=', stringifyCookieValue(value),
 69                 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
 70                 options.path    ? '; path=' + options.path : '',
 71                 options.domain  ? '; domain=' + options.domain : '',
 72                 options.secure  ? '; secure' : ''
 73             ].join(''));
 74         }
 75 
 76         // Read
 77 
 78         var result = key ? undefined : {};
 79 
 80         // To prevent the for loop in the first place assign an empty array
 81         // in case there are no cookies at all. Also prevents odd result when
 82         // calling $.cookie().
 83         var cookies = document.cookie ? document.cookie.split('; ') : [];
 84 
 85         for (var i = 0, l = cookies.length; i < l; i++) {
 86             var parts = cookies[i].split('=');
 87             var name = decode(parts.shift());
 88             var cookie = parts.join('=');
 89 
 90             if (key && key === name) {
 91                 // If second argument (value) is a function it's a converter...
 92                 result = read(cookie, value);
 93                 break;
 94             }
 95 
 96             // Prevent storing a cookie that we couldn't decode.
 97             if (!key && (cookie = read(cookie)) !== undefined) {
 98                 result[name] = cookie;
 99             }
100         }
101 
102         return result;
103     };
104 
105     config.defaults = {};
106 
107     $.removeCookie = function (key, options) {
108         if ($.cookie(key) === undefined) {
109             return false;
110         }
111 
112         // Must not alter options, thus extending a fresh object...
113         $.cookie(key, '', $.extend({}, options, { expires: -1 }));
114         return !$.cookie(key);
115     };
116 
117 }));
jQuery Cookie Plugin v1.4.1
原文地址:https://www.cnblogs.com/ujq3/p/8059904.html