-/*jshint eqnull:true */
/*!
- * jQuery Cookie Plugin v1.2
+ * jQuery Cookie Plugin v1.3.1
* https://github.com/carhartl/jquery-cookie
*
- * Copyright 2011, Klaus Hartl
- * Dual licensed under the MIT or GPL Version 2 licenses.
- * http://www.opensource.org/licenses/mit-license.php
- * http://www.opensource.org/licenses/GPL-2.0
+ * Copyright 2013 Klaus Hartl
+ * Released under the MIT license
*/
(function ($, document, undefined) {
}
function decoded(s) {
- return decodeURIComponent(s.replace(pluses, ' '));
+ return unRfc2068(decodeURIComponent(s.replace(pluses, ' ')));
}
- $.cookie = function (key, value, options) {
+ function unRfc2068(value) {
+ if (value.indexOf('"') === 0) {
+ // This is a quoted cookie as according to RFC2068, unescape
+ value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
+ }
+ return value;
+ }
+
+ function fromJSON(value) {
+ return config.json ? JSON.parse(value) : value;
+ }
- // key and at least value given, set cookie...
- if (value !== undefined && !/Object/.test(Object.prototype.toString.call(value))) {
- options = $.extend({}, $.cookie.defaults, options);
+ var config = $.cookie = function (key, value, options) {
+
+ // write
+ if (value !== undefined) {
+ options = $.extend({}, config.defaults, options);
if (value === null) {
options.expires = -1;
t.setDate(t.getDate() + days);
}
- value = String(value);
+ value = config.json ? JSON.stringify(value) : String(value);
return (document.cookie = [
- encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
+ encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
].join(''));
}
- // key and possibly options given, get cookie...
- options = value || $.cookie.defaults || {};
- var decode = options.raw ? raw : decoded;
+ // read
+ var decode = config.raw ? raw : decoded;
var cookies = document.cookie.split('; ');
- for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
- if (decode(parts.shift()) === key) {
- return decode(parts.join('='));
+ var result = key ? null : {};
+ for (var i = 0, l = cookies.length; i < l; i++) {
+ var parts = cookies[i].split('=');
+ var name = decode(parts.shift());
+ var cookie = decode(parts.join('='));
+
+ if (key && key === name) {
+ result = fromJSON(cookie);
+ break;
+ }
+
+ if (!key) {
+ result[name] = fromJSON(cookie);
}
}
- return null;
+ return result;
};
- $.cookie.defaults = {};
+ config.defaults = {};
$.removeCookie = function (key, options) {
- if ($.cookie(key, options) !== null) {
+ if ($.cookie(key) !== null) {
$.cookie(key, null, options);
return true;
}