Skip to content

Commit 91e60cb

Browse files
committed
Merge branch 'master' of github.com:carhartl/jquery-cookie
2 parents 94557e8 + f43512e commit 91e60cb

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Create expiring cookie, valid across entire page:
2525
Read cookie:
2626

2727
$.cookie('the_cookie'); // => 'the_value'
28+
$.cookie('the_cookie', {raw: true}); // => 'the_value' NOT URL decoded
2829
$.cookie('not_existing'); // => null
2930

3031
Delete cookie by passing null as value:
@@ -35,6 +36,8 @@ Delete cookie by passing null as value:
3536

3637
## Options
3738

39+
Options can be set globally by setting properties of the `$.cookie.defaults` object or individually for each call to `$.cookie()` by passing a plain object to the options argument. Per-call options override the ones set by `$.cookie.defaults`.
40+
3841
expires: 365
3942

4043
Define lifetime of the cookie. Value can be a `Number` (which will be interpreted as days from time of creation) or a `Date` object. If omitted, the cookie becomes a session cookie.

‎jquery.cookie.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// key and at least value given, set cookie...
1414
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
15-
options = $.extend({}, options);
15+
options = $.extend({}, $.cookie.defaults, options);
1616

1717
if (value === null || value === undefined) {
1818
options.expires = -1;
@@ -35,7 +35,7 @@
3535
}
3636

3737
// key and possibly options given, get cookie...
38-
options = value || {};
38+
options = value || $.cookie.defaults || {};
3939
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
4040

4141
var cookies = document.cookie.split('; ');
@@ -46,4 +46,7 @@
4646
}
4747
return null;
4848
};
49+
50+
$.cookie.defaults = {};
51+
4952
})(jQuery);

‎test.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ test('raw: true', 1, function () {
3535
document.cookie = 'c=%20v';
3636
equal($.cookie('c', { raw: true }), '%20v', 'should not decode');
3737
});
38+
test('$.cookie.defaults.raw = true', 1, function () {
39+
document.cookie = 'c=%20v';
40+
$.cookie.defaults.raw = true;
41+
equal($.cookie('c'), '%20v', 'should not decode');
42+
$.cookie.defaults.raw = false;
43+
});
3844

3945
test('embedded equals', 1, function() {
4046
$.cookie('c', 'foo=bar', { raw: true });
@@ -73,6 +79,13 @@ test('raw: true', 1, function () {
7379
' v', 'should not encode');
7480
});
7581

82+
test('$.cookie.defaults.raw = true', 1, function () {
83+
$.cookie.defaults.raw = true;
84+
equal($.cookie('c', ' v').split('=')[1],
85+
' v', 'should not encode');
86+
$.cookie.defaults.raw = false;
87+
});
88+
7689

7790
module('delete', before);
7891

0 commit comments

Comments
 (0)