Merge with @h5bp
[ReadingsJQM.git] / js / jquery.cookie.js
1 /*!
2  * jQuery Cookie Plugin
3  * https://github.com/carhartl/jquery-cookie
4  *
5  * Copyright 2011, Klaus Hartl
6  * Dual licensed under the MIT or GPL Version 2 licenses.
7  * http://www.opensource.org/licenses/mit-license.php
8  * http://www.opensource.org/licenses/GPL-2.0
9  */
10 (function($) {
11     $.cookie = function(key, value, options) {
12
13         // key and at least value given, set cookie...
14         if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
15             options = $.extend({}, options);
16
17             if (value === null || value === undefined) {
18                 options.expires = -1;
19             }
20
21             if (typeof options.expires === 'number') {
22                 var days = options.expires, t = options.expires = new Date();
23                 t.setDate(t.getDate() + days);
24             }
25
26             value = String(value);
27
28             return (document.cookie = [
29                 encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
30                 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
31                 options.path    ? '; path=' + options.path : '',
32                 options.domain  ? '; domain=' + options.domain : '',
33                 options.secure  ? '; secure' : ''
34             ].join(''));
35         }
36
37         // key and possibly options given, get cookie...
38         options = value || {};
39         var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
40
41         var pairs = document.cookie.split('; ');
42         for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
43             if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
44         }
45         return null;
46     };
47 })(jQuery);