Index: trunk/phase3/resources/jquery/jquery.cookie.js |
— | — | @@ -1,19 +1,15 @@ |
2 | | -/*jslint browser: true */ /*global jQuery: true */ |
3 | | - |
4 | 2 | /** |
5 | | - * jQuery Cookie plugin |
| 3 | + * Cookie plugin |
6 | 4 | * |
7 | | - * Copyright (c) 2010 Klaus Hartl (stilbuero.de) |
| 5 | + * Copyright (c) 2006 Klaus Hartl (stilbuero.de) |
8 | 6 | * Dual licensed under the MIT and GPL licenses: |
9 | 7 | * http://www.opensource.org/licenses/mit-license.php |
10 | 8 | * http://www.gnu.org/licenses/gpl.html |
11 | 9 | * |
12 | 10 | */ |
13 | 11 | |
14 | | -// TODO JsDoc |
15 | | - |
16 | 12 | /** |
17 | | - * Create a cookie with the given key and value and other optional parameters. |
| 13 | + * Create a cookie with the given name and value and other optional parameters. |
18 | 14 | * |
19 | 15 | * @example $.cookie('the_cookie', 'the_value'); |
20 | 16 | * @desc Set the value of a cookie. |
— | — | @@ -25,16 +21,16 @@ |
26 | 22 | * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain |
27 | 23 | * used when the cookie was set. |
28 | 24 | * |
29 | | - * @param key String The key of the cookie. |
30 | | - * @param value String The value of the cookie. |
31 | | - * @param options Object An object literal containing key/value pairs to provide optional cookie attributes. |
32 | | - * @option expires Number|Date Either an integer specifying the expiration date from now on in days or a Date object. |
| 25 | + * @param String name The name of the cookie. |
| 26 | + * @param String value The value of the cookie. |
| 27 | + * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. |
| 28 | + * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. |
33 | 29 | * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. |
34 | 30 | * If set to null or omitted, the cookie will be a session cookie and will not be retained |
35 | 31 | * when the the browser exits. |
36 | | - * @option path String The value of the path atribute of the cookie (default: path of page that created the cookie). |
37 | | - * @option domain String The value of the domain attribute of the cookie (default: domain of page that created the cookie). |
38 | | - * @option secure Boolean If true, the secure attribute of the cookie will be set and the cookie transmission will |
| 32 | + * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). |
| 33 | + * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). |
| 34 | + * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will |
39 | 35 | * require a secure protocol (like HTTPS). |
40 | 36 | * @type undefined |
41 | 37 | * |
— | — | @@ -44,12 +40,12 @@ |
45 | 41 | */ |
46 | 42 | |
47 | 43 | /** |
48 | | - * Get the value of a cookie with the given key. |
| 44 | + * Get the value of a cookie with the given name. |
49 | 45 | * |
50 | 46 | * @example $.cookie('the_cookie'); |
51 | 47 | * @desc Get the value of a cookie. |
52 | 48 | * |
53 | | - * @param key String The key of the cookie. |
| 49 | + * @param String name The name of the cookie. |
54 | 50 | * @return The value of the cookie. |
55 | 51 | * @type String |
56 | 52 | * |
— | — | @@ -57,33 +53,44 @@ |
58 | 54 | * @cat Plugins/Cookie |
59 | 55 | * @author Klaus Hartl/klaus.hartl@stilbuero.de |
60 | 56 | */ |
61 | | -jQuery.cookie = function (key, value, options) { |
62 | | - |
63 | | - // key and value given, set cookie... |
64 | | - if (arguments.length > 1 && (value === null || typeof value !== "object")) { |
65 | | - options = jQuery.extend({}, options); |
66 | | - |
| 57 | +jQuery.cookie = function(name, value, options) { |
| 58 | + if (typeof value != 'undefined') { // name and value given, set cookie |
| 59 | + options = options || {}; |
67 | 60 | if (value === null) { |
| 61 | + value = ''; |
68 | 62 | options.expires = -1; |
69 | 63 | } |
70 | | - |
71 | | - if (typeof options.expires === 'number') { |
72 | | - var days = options.expires, t = options.expires = new Date(); |
73 | | - t.setDate(t.getDate() + days); |
| 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 | 74 | } |
75 | | - |
76 | | - return (document.cookie = [ |
77 | | - encodeURIComponent(key), '=', |
78 | | - options.raw ? String(value) : encodeURIComponent(String(value)), |
79 | | - options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
80 | | - options.path ? '; path=' + options.path : '', |
81 | | - options.domain ? '; domain=' + options.domain : '', |
82 | | - options.secure ? '; secure' : '' |
83 | | - ].join('')); |
| 75 | + // CAUTION: 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 | + } |
| 94 | + } |
| 95 | + return cookieValue; |
84 | 96 | } |
85 | | - |
86 | | - // key and possibly options given, get cookie... |
87 | | - options = value || {}; |
88 | | - var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent; |
89 | | - return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null; |
90 | 97 | }; |