Fix: update jquery-cookie plugin

This commit is contained in:
Regis Houssin 2012-06-11 17:24:32 +02:00
parent c9be1cbb8b
commit fb15ea20c6

View File

@ -1,96 +1,50 @@
/** /*!
* Cookie plugin * jQuery Cookie Plugin
* * https://github.com/carhartl/jquery-cookie
* Copyright (c) 2006 Klaus Hartl (stilbuero.de) *
* Dual licensed under the MIT and GPL licenses: * Copyright 2011, Klaus Hartl
* http://www.opensource.org/licenses/mit-license.php * Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.gnu.org/licenses/gpl.html * http://www.opensource.org/licenses/mit-license.php
* * http://www.opensource.org/licenses/GPL-2.0
*/ */
(function($) {
/** $.cookie = function(key, value, options) {
* Create a cookie with the given name and value and other optional parameters.
* // key and at least value given, set cookie...
* @example $.cookie('the_cookie', 'the_value'); if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
* @desc Set the value of a cookie. options = $.extend({}, options);
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options. if (value === null || value === undefined) {
* @example $.cookie('the_cookie', 'the_value'); options.expires = -1;
* @desc Create a session cookie. }
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain if (typeof options.expires === 'number') {
* used when the cookie was set. var days = options.expires, t = options.expires = new Date();
* t.setDate(t.getDate() + days);
* @param String name The name of the cookie. }
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes. value = String(value);
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted. return (document.cookie = [
* If set to null or omitted, the cookie will be a session cookie and will not be retained encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
* when the the browser exits. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). options.path ? '; path=' + options.path : '',
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). options.domain ? '; domain=' + options.domain : '',
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will options.secure ? '; secure' : ''
* require a secure protocol (like HTTPS). ].join(''));
* @type undefined }
*
* @name $.cookie // key and possibly options given, get cookie...
* @cat Plugins/Cookie options = value || {};
* @author Klaus Hartl/klaus.hartl@stilbuero.de var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
*/
var pairs = document.cookie.split('; ');
/** for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
* Get the value of a cookie with the given name. 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
* }
* @example $.cookie('the_cookie'); return null;
* @desc Get the value of a cookie. };
* $.removeCookie = function(cookieName) {
* @param String name The name of the cookie. $.cookie(cookieName, null);
* @return The value of the cookie. }
* @type String })(jQuery);
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};