Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit ecb6962

Browse files
author
Eric Zieger
authored
Merge pull request #3 from theZieger/dev
fixes problems in dev environments and string compat
2 parents 69c4821 + b7d07ff commit ecb6962

3 files changed

Lines changed: 9 additions & 27 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# cookiejs v0.4.0
1+
# cookiejs v0.4.1
22

33
> Set, get and remove cookies.
44
@@ -26,7 +26,7 @@ cookiejs.set(
2626
'captainObvious',
2727
'Thank you Captain Obvious, you just saved my life.',
2828
{
29-
domain: '.eric-zieger.de', // default: '.domain.tld',
29+
domain: '.eric-zieger.de', // default: 'subdomain.current-domain.tld',
3030
path: '/the-adventures-of-captain-obvious/', // default: '/',
3131
expires: sYourUTCString, // default: undefined (session cookie)
3232
max-age: 60 // max-age-in-seconds, // default: undefined

cookiejs.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@
1616
*/
1717
var cookiejs = {};
1818

19-
/**
20-
* gets the current document.domain and returns .DOMAIN.TLD to make cookie accessable over all subdomains
21-
*
22-
* @private
23-
*
24-
* @returns {String} representation of ".DOMAIN.TLD" of document.domain
25-
*/
26-
var getCookieDomain = function() {
27-
return '.' + document.domain.split('.').slice(-2).join('.');
28-
};
29-
3019
/**
3120
* sets or overwrites a cookie
3221
*
@@ -39,24 +28,17 @@
3928
cookiejs.set = function(sCookieName, sValue, oAttributes) {
4029
var sAttributes = '';
4130

42-
oAttributes = oAttributes || {
43-
domain: '; domain=' + getCookieDomain(),
44-
path: '; path=/'
45-
};
46-
47-
if (oAttributes['domain'] === undefined) {
48-
sAttributes += '; domain=' + getCookieDomain();
49-
}
31+
oAttributes = oAttributes || {};
5032

51-
if (oAttributes['path'] === undefined) {
33+
if (typeof oAttributes.path !== 'string') {
5234
sAttributes += '; path=/';
5335
}
5436

5537
Object.keys(oAttributes).forEach(function(sAttributeName) {
5638
sAttributes += ';' + sAttributeName + '=' + oAttributes[sAttributeName];
57-
});
39+
});
5840

59-
document.cookie = sCookieName + '=' + sValue + sAttributes;
41+
document.cookie = encodeURIComponent(sCookieName) + '=' + encodeURIComponent(sValue) + sAttributes;
6042
};
6143

6244
/**
@@ -79,8 +61,8 @@
7961

8062
for (var i = aCookies.length - 1; i >= 0; i--) {
8163
aCookie = aCookies[i].split('=');
82-
if (aCookie[0] === sCookieName) {
83-
gCookieValue = aCookie[1];
64+
if (decodeURIComponent(aCookie[0]) === sCookieName) {
65+
gCookieValue = decodeURIComponent(aCookie[1]);
8466
}
8567
}
8668

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cookiejs.js",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Set, get and remove cookies.",
55
"main": "cookiejs.js",
66
"scripts": {

0 commit comments

Comments
 (0)