Skip to content

Commit a105e27

Browse files
tomsouthallFagnerMartinsBrack
authored andcommitted
Add ".noConflict()" to prevent conflict with third party scripts
Closes js-cookiegh-24.
1 parent 2f625eb commit a105e27

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ Cookies.remove('name', { path: '/' }); // => true
8282

8383
*Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.*
8484

85+
## Namespace conflicts
86+
87+
If there is any danger of a conflict with the namespace `Cookies`, the `noConflict` method will allow you to define a new namespace and preserve the original one. This is especially useful when running the script on third party sites e.g. as part of a widget or SDK.
88+
89+
```javascript
90+
// Assign the js-cookie api to a different variable and restore the original "window.Cookies"
91+
var Cookies2 = Cookies.noConflict();
92+
Cookies2.set('name', 'value');
93+
```
94+
95+
*Note: The `.noConflict` method is not necessary when using AMD or CommonJS, thus it is not exposed in those environments.*
96+
8597
## Configuration
8698

8799
### raw

src/js.cookie.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
module.exports = factory(jQuery);
1919
} else {
2020
// Browser globals
21-
window.Cookies = factory(window.jQuery);
21+
var _OldCookies = window.Cookies;
22+
var api = window.Cookies = factory(window.jQuery);
23+
api.noConflict = function() {
24+
window.Cookies = _OldCookies;
25+
return api;
26+
};
2227
}
2328
}(function ($) {
2429

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>JavaScript Cookie Test Suite</title>
66
<link href="http://code.jquery.com/qunit/qunit-1.14.0.css" rel="stylesheet">
77
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
8+
<script>Cookies = 'existent global'</script>
89
<script src="../src/js.cookie.js"></script>
910
<script src="polyfill.js"></script>
1011
<script src="tests.js"></script>

test/tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,15 @@ test('read converter with raw = true', function() {
362362
Cookies.set('c', '1');
363363
strictEqual(Cookies.get('c', Number), 1, 'does not decode, but converts read value');
364364
});
365+
366+
module('noConflict', lifecycle);
367+
368+
test('do not conflict with existent globals', function() {
369+
expect(2);
370+
var Cookies = window.Cookies.noConflict();
371+
Cookies.set('c', 'v');
372+
strictEqual(Cookies.get('c'), 'v', 'should work correctly');
373+
strictEqual(window.Cookies, 'existent global', 'should restore the original global');
374+
window.Cookies = Cookies;
375+
});
376+

0 commit comments

Comments
 (0)