Skip to content

Commit dfec311

Browse files
committed
Setup jquery-cookie in a closure
Referring to the jQuery variable later may fail if jQuery.noConflict is used, so wrap the plugin in a closure
1 parent 7e8d8af commit dfec311

1 file changed

Lines changed: 28 additions & 26 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@
77
* http://www.gnu.org/licenses/gpl.html
88
*
99
*/
10-
jQuery.cookie = function (key, value, options) {
10+
(function($) {
11+
$.cookie = function (key, value, options) {
1112

12-
// key and at least value given, set cookie...
13-
if (arguments.length > 1 && String(value) !== "[object Object]") {
14-
options = jQuery.extend({}, options);
13+
// key and at least value given, set cookie...
14+
if (arguments.length > 1 && String(value) !== "[object Object]") {
15+
options = $.extend({}, options);
1516

16-
if (value === null || value === undefined) {
17-
options.expires = -1;
18-
}
17+
if (value === null || value === undefined) {
18+
options.expires = -1;
19+
}
1920

20-
if (typeof options.expires === 'number') {
21-
var days = options.expires, t = options.expires = new Date();
22-
t.setDate(t.getDate() + days);
23-
}
21+
if (typeof options.expires === 'number') {
22+
var days = options.expires, t = options.expires = new Date();
23+
t.setDate(t.getDate() + days);
24+
}
2425

25-
value = String(value);
26+
value = String(value);
2627

27-
return (document.cookie = [
28-
encodeURIComponent(key), '=',
29-
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-
}
28+
return (document.cookie = [
29+
encodeURIComponent(key), '=',
30+
options.raw ? value : encodeURIComponent(value),
31+
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
32+
options.path ? '; path=' + options.path : '',
33+
options.domain ? '; domain=' + options.domain : '',
34+
options.secure ? '; secure' : ''
35+
].join(''));
36+
}
3637

37-
// key and possibly options given, get cookie...
38-
options = value || {};
39-
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
40-
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
41-
};
38+
// key and possibly options given, get cookie...
39+
options = value || {};
40+
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
41+
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
42+
};
43+
})(jQuery);

0 commit comments

Comments
 (0)