Skip to content

Commit bccdaf7

Browse files
Do not parse as JSON when window has a truthy 'json' prop
Closes js-cookiegh-250. Closes js-cookiegh-238.
1 parent 9b46741 commit bccdaf7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/js.cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127

128128
api.set = api;
129129
api.get = function (key) {
130-
return api(key);
130+
return api.call(api, key);
131131
};
132132
api.getJSON = function () {
133133
return api.apply({

test/tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ QUnit.test('Call to read cookie when passing an Object Literal as the second arg
125125
assert.strictEqual(document.cookie, '', 'should not create a cookie');
126126
});
127127

128+
// github.com/js-cookie/js-cookie/issues/238
129+
QUnit.test('Call to read cookie when there is a window.json variable globally', function (assert) {
130+
assert.expect(1);
131+
window.json = true;
132+
Cookies.set('boolean', true);
133+
assert.strictEqual(typeof Cookies.get('boolean'), 'string', 'should not change the returned type');
134+
// IE 6-8 throw an exception if trying to delete a window property
135+
// See stackoverflow.com/questions/1073414/deleting-a-window-property-in-ie/1824228
136+
try {
137+
delete window.json;
138+
} catch (e) {}
139+
});
140+
128141
QUnit.module('write', lifecycle);
129142

130143
QUnit.test('String primitive', function (assert) {

0 commit comments

Comments
 (0)