Skip to content

Commit c32ed09

Browse files
committed
Calling $.cookie without arguments returns all available cookies as object...
1 parent 57cd3dd commit c32ed09

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ HEAD
1414

1515
- Properly handle RFC 2068 quoted cookie values.
1616

17+
- `$.cookie()` returns all available cookies.
18+
1719
1.2.0
1820
-----
1921
- Adding `$.removeCookie('foo')` for deleting a cookie, using `$.cookie('foo', null)` is now deprecated.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Read cookie:
2929

3030
$.cookie('the_cookie'); // => "the_value"
3131
$.cookie('not_existing'); // => null
32+
33+
Read all available cookies:
34+
35+
$.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }
3236

3337
Delete cookie:
3438

jquery.cookie.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,27 @@
5454
// read
5555
var decode = config.raw ? raw : decoded;
5656
var cookies = document.cookie.split('; ');
57+
var result = key ? null : {};
5758
for (var i = 0, l = cookies.length; i < l; i++) {
5859
var parts = cookies[i].split('=');
59-
if (decode(parts.shift()) === key) {
60-
var cookie = decode(parts.join('='));
61-
return config.json ? JSON.parse(cookie) : cookie;
60+
var name = decode(parts.shift());
61+
var cookie = decode(parts.join('='));
62+
63+
if (config.json) {
64+
cookie = JSON.parse(cookie);
65+
}
66+
67+
if (key && key === name) {
68+
result = cookie;
69+
break;
70+
}
71+
72+
if (!key) {
73+
result[name] = cookie;
6274
}
6375
}
6476

65-
return null;
77+
return result;
6678
};
6779

6880
config.defaults = {};

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ test('json: true', function () {
8181
}
8282
});
8383

84+
+test('no arguments', function () {
85+
document.cookie = 'x=y';
86+
var count = document.cookie.split(';').length;
87+
var cookies = $.cookie();
88+
equal(typeof cookies, 'object', 'should return object');
89+
var found = 0;
90+
for (var key in cookies) { found++; }
91+
equal(found, count, 'should contain all cookies');
92+
equal(cookies.x, 'y', 'should contain cookie values');
93+
});
94+
8495
asyncTest('malformed cookie value in IE (#88, #117)', function() {
8596
expect(1);
8697
// Sandbox in an iframe so that we can poke around with document.cookie.

0 commit comments

Comments
 (0)