Skip to content

Commit a38840b

Browse files
Merge branch 'master' into 2.0
Conflicts: README.md src/js.cookie.js
2 parents 37d41c5 + a105e27 commit a38840b

5 files changed

Lines changed: 45 additions & 15 deletions

File tree

.travis.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
language: node_js
2-
node_js:
3-
- '0.10'
4-
before_script:
5-
- npm install -g grunt-cli
6-
# Only use grunt-ci for commits pushed to this repo. Fall back to regular test
7-
# for pull requests (as secure variables won't be exposed there).
8-
script:
9-
- ./travis.sh
10-
env:
11-
# Encrypted SAUCE_USERNAME and SAUCE_ACCESS_KEY used by travis
12-
global:
13-
- secure: IkMOa/8r4sWyzUMxecsfqoPzZyIqVAMwPkQ6/HxXPbT8X7UnvqAdaicAMeHEKtOnOac+rx6pGB9HQvC8P/ZzkEBtsKLP4nEh9vsAInZvb3pXg+qbIgIK6/19X0kU4UkpDqVdWmBuFTamJvMDMstUTgEaM3869bB5vGp9taBgfVo=
14-
- secure: DKrQplF0CBiBh+cbQ8D7EKebCeklUWEELblIJdU4475Occ4G9b8ZFYO9HFwl1B8F/XapB7CsMyxbJCWor030FySeqn8bhJs9NoAVoYGg+MtWniv1EOHuZLWuOGfgQDv7qj5U0Af9Y655MmUpXSN2aDlCmQweWnYdpFTM9Dfsdd8=
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
before_script:
5+
- npm install -g grunt-cli
6+
# Only use grunt-ci for commits pushed to this repo. Fall back to regular test
7+
# for pull requests (as secure variables won't be exposed there).
8+
script:
9+
- ./travis.sh
10+
env:
11+
# Encrypted SAUCE_USERNAME and SAUCE_ACCESS_KEY used by travis
12+
global:
13+
- secure: IkMOa/8r4sWyzUMxecsfqoPzZyIqVAMwPkQ6/HxXPbT8X7UnvqAdaicAMeHEKtOnOac+rx6pGB9HQvC8P/ZzkEBtsKLP4nEh9vsAInZvb3pXg+qbIgIK6/19X0kU4UkpDqVdWmBuFTamJvMDMstUTgEaM3869bB5vGp9taBgfVo=
14+
- secure: DKrQplF0CBiBh+cbQ8D7EKebCeklUWEELblIJdU4475Occ4G9b8ZFYO9HFwl1B8F/XapB7CsMyxbJCWor030FySeqn8bhJs9NoAVoYGg+MtWniv1EOHuZLWuOGfgQDv7qj5U0Af9Y655MmUpXSN2aDlCmQweWnYdpFTM9Dfsdd8=

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ Cookies.remove('name', { path: '/' }); // removed!
8686

8787
*IMPORTANT! when deleting a cookie, you must pass the exact same path, domain and secure attributes that were used to set the cookie, unless you're relying on the [default attributes](#cookie-attributes).*
8888

89+
## Namespace conflicts
90+
91+
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.
92+
93+
```javascript
94+
// Assign the js-cookie api to a different variable and restore the original "window.Cookies"
95+
var Cookies2 = Cookies.noConflict();
96+
Cookies2.set('name', 'value');
97+
```
98+
99+
*Note: The `.noConflict` method is not necessary when using AMD or CommonJS, thus it is not exposed in those environments.*
100+
89101
## JSON
90102

91103
js-cookie provides automatic JSON storage for cookies.

src/js.cookie.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
module.exports = factory();
1515
} else {
1616
// Browser globals
17-
window.Cookies = factory();
17+
var _OldCookies = window.Cookies;
18+
var api = window.Cookies = factory(window.jQuery);
19+
api.noConflict = function() {
20+
window.Cookies = _OldCookies;
21+
return api;
22+
};
1823
}
1924
}(function () {
2025
var unallowedChars = {

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
@@ -425,3 +425,15 @@ test('Call to read all cookies with mixed json', function () {
425425
deepEqual(Cookies.getJSON(), { c: { foo: 'bar' }, c2: 'v' }, 'returns JSON parsed cookies');
426426
deepEqual(Cookies.get(), { c: '{"foo":"bar"}', c2: 'v' }, 'returns unparsed cookies');
427427
});
428+
429+
module('noConflict', lifecycle);
430+
431+
test('do not conflict with existent globals', function() {
432+
expect(2);
433+
var Cookies = window.Cookies.noConflict();
434+
Cookies.set('c', 'v');
435+
strictEqual(Cookies.get('c'), 'v', 'should work correctly');
436+
strictEqual(window.Cookies, 'existent global', 'should restore the original global');
437+
window.Cookies = Cookies;
438+
});
439+

0 commit comments

Comments
 (0)