Skip to content

Commit f98f6a3

Browse files
committed
fix incorrect return value in IE (null) in case cookie value is empty string, closes js-cookie#25
1 parent aa977dd commit f98f6a3

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@
3737

3838
// key and possibly options given, get cookie...
3939
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;
40+
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
41+
42+
var parts = document.cookie.split('; ');
43+
for (var i = 0, part; part = parts[i]; i++) {
44+
var pair = part.split('=');
45+
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB
46+
}
47+
return null;
4248
};
4349
})(jQuery);

‎test.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ test('simple value', 1, function () {
1515
equals($.cookie('c'), 'v', 'should return value');
1616
});
1717

18+
test('empty value', 1, function () {
19+
$.cookie('c', '');
20+
equals($.cookie('c'), '', 'should return value');
21+
});
22+
1823
test('not existing', 1, function () {
1924
equals($.cookie('whatever'), null, 'should return null');
2025
});

0 commit comments

Comments
 (0)