1+ /*jslint browser: true */ /*global jQuery: true */
2+
13/**
24 * Cookie plugin
35 *
4- * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
6+ * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
57 * Dual licensed under the MIT and GPL licenses:
68 * http://www.opensource.org/licenses/mit-license.php
79 * http://www.gnu.org/licenses/gpl.html
810 *
911 */
1012
13+ // TODO JsDoc
14+
1115/**
12- * Create a cookie with the given name and value and other optional parameters.
16+ * Create a cookie with the given key and value and other optional parameters.
1317 *
1418 * @example $.cookie('the_cookie', 'the_value');
1519 * @desc Set the value of a cookie.
2125 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
2226 * used when the cookie was set.
2327 *
24- * @param String name The name of the cookie.
28+ * @param String key The key of the cookie.
2529 * @param String value The value of the cookie.
2630 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
2731 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
4044 */
4145
4246/**
43- * Get the value of a cookie with the given name .
47+ * Get the value of a cookie with the given key .
4448 *
4549 * @example $.cookie('the_cookie');
4650 * @desc Get the value of a cookie.
4751 *
48- * @param String name The name of the cookie.
52+ * @param String key The key of the cookie.
4953 * @return The value of the cookie.
5054 * @type String
5155 *
5256 * @name $.cookie
5357 * @cat Plugins/Cookie
5458 * @author Klaus Hartl/klaus.hartl@stilbuero.de
5559 */
56- jQuery . cookie = function ( name , value , options ) {
57- if ( typeof value != 'undefined' ) { // name and value given, set cookie
58- options = options || { } ;
60+ jQuery . cookie = function ( key , value , options ) {
61+
62+ // key and value given, set cookie...
63+ if ( arguments . length > 1 ) {
64+ options = jQuery . extend ( { } , options ) ;
65+
5966 if ( value === null ) {
60- value = '' ;
61- options = $ . extend ( { } , options ) ; // clone object since it's unexpected behavior if the expired property were changed
6267 options . expires = - 1 ;
6368 }
64- var expires = '' ;
65- if ( options . expires && ( typeof options . expires == 'number' || options . expires . toUTCString ) ) {
66- var date ;
67- if ( typeof options . expires == 'number' ) {
68- date = new Date ( ) ;
69- date . setTime ( date . getTime ( ) + ( options . expires * 24 * 60 * 60 * 1000 ) ) ;
70- } else {
71- date = options . expires ;
72- }
73- expires = '; expires=' + date . toUTCString ( ) ; // use expires attribute, max-age is not supported by IE
74- }
75- // NOTE Needed to parenthesize options.path and options.domain
76- // in the following expressions, otherwise they evaluate to undefined
77- // in the packed version for some reason...
78- var path = options . path ? '; path=' + ( options . path ) : '' ;
79- var domain = options . domain ? '; domain=' + ( options . domain ) : '' ;
80- var secure = options . secure ? '; secure' : '' ;
81- document . cookie = [ name , '=' , encodeURIComponent ( value ) , expires , path , domain , secure ] . join ( '' ) ;
82- } else { // only name given, get cookie
83- var cookieValue = null ;
84- if ( document . cookie && document . cookie != '' ) {
85- var cookies = document . cookie . split ( ';' ) ;
86- for ( var i = 0 ; i < cookies . length ; i ++ ) {
87- var cookie = jQuery . trim ( cookies [ i ] ) ;
88- // Does this cookie string begin with the name we want?
89- if ( cookie . substring ( 0 , name . length + 1 ) == ( name + '=' ) ) {
90- cookieValue = decodeURIComponent ( cookie . substring ( name . length + 1 ) ) ;
91- break ;
92- }
93- }
69+
70+ if ( typeof options . expires === 'number' ) {
71+ var days = options . expires , t = options . expires = new Date ( ) ;
72+ t . setDate ( t . getDate ( ) + days ) ;
9473 }
95- return cookieValue ;
74+
75+ return ( document . cookie = [ key , '=' , encodeURIComponent ( String ( value ) ) ,
76+ options . expires ? '; expires=' + options . expires . toUTCString ( ) : '' , // use expires attribute, max-age is not supported by IE
77+ options . path ? '; path=' + options . path : '' ,
78+ options . domain ? '; domain=' + options . domain : '' ,
79+ options . secure ? '; secure' : ''
80+ ] . join ( '' ) ) ;
9681 }
97- } ;
82+
83+ // get cookie...
84+ var result ;
85+ return ( result = new RegExp ( '(?:^|; )' + key + '=([^;]*)' ) . exec ( document . cookie ) ) ? decodeURIComponent ( result [ 1 ] ) : null ;
86+ } ;
0 commit comments