95001fb0d4bc62fdf6c64d4f5c9c3e8ac6e02a5d
[lhc/web/wiklou.git] / resources / lib / jquery / jquery.cookie.js
1 /*jshint eqnull:true */
2 /*!
3 * jQuery Cookie Plugin v1.2
4 * https://github.com/carhartl/jquery-cookie
5 *
6 * Copyright 2011, Klaus Hartl
7 * Dual licensed under the MIT or GPL Version 2 licenses.
8 * http://www.opensource.org/licenses/mit-license.php
9 * http://www.opensource.org/licenses/GPL-2.0
10 */
11 (function ($, document, undefined) {
12
13 var pluses = /\+/g;
14
15 function raw(s) {
16 return s;
17 }
18
19 function decoded(s) {
20 return decodeURIComponent(s.replace(pluses, ' '));
21 }
22
23 $.cookie = function (key, value, options) {
24
25 // key and at least value given, set cookie...
26 if (value !== undefined && !/Object/.test(Object.prototype.toString.call(value))) {
27 options = $.extend({}, $.cookie.defaults, options);
28
29 if (value === null) {
30 options.expires = -1;
31 }
32
33 if (typeof options.expires === 'number') {
34 var days = options.expires, t = options.expires = new Date();
35 t.setDate(t.getDate() + days);
36 }
37
38 value = String(value);
39
40 return (document.cookie = [
41 encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
42 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
43 options.path ? '; path=' + options.path : '',
44 options.domain ? '; domain=' + options.domain : '',
45 options.secure ? '; secure' : ''
46 ].join(''));
47 }
48
49 // key and possibly options given, get cookie...
50 options = value || $.cookie.defaults || {};
51 var decode = options.raw ? raw : decoded;
52 var cookies = document.cookie.split('; ');
53 for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
54 if (decode(parts.shift()) === key) {
55 return decode(parts.join('='));
56 }
57 }
58
59 return null;
60 };
61
62 $.cookie.defaults = {};
63
64 $.removeCookie = function (key, options) {
65 if ($.cookie(key, options) !== null) {
66 $.cookie(key, null, options);
67 return true;
68 }
69 return false;
70 };
71
72 })(jQuery, document);