Skip to content

Commit a174ebe

Browse files
Merge pull request js-cookie#17 from js-cookie/default-behavior-docs
Documentation enhancement
2 parents fbde23b + 4689dc4 commit a174ebe

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

README.md

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ in Internet Explorer on Windows 7 for instance (because of the wrong MIME type).
2222

2323
The plugin can also be loaded as AMD or CommonJS module.
2424

25-
## Usage
25+
## Basic Usage
2626

27-
Create session cookie:
27+
Create a session cookie, valid to the current page:
2828

2929
```javascript
3030
Cookies.set('name', 'value');
3131
```
3232

33-
Create expiring cookie, 7 days from then:
33+
Create a cookie that expires 7 days from now, valid to the current page:
3434

3535
```javascript
3636
Cookies.set('name', 'value', { expires: 7 });
3737
```
3838

39-
Create expiring cookie, valid across entire site:
39+
Create an expiring cookie, valid across the entire site:
4040

4141
```javascript
4242
Cookies.set('name', 'value', { expires: 7, path: '/' });
@@ -62,15 +62,13 @@ Delete cookie:
6262
Cookies.remove('name'); // => true
6363
Cookies.remove('nothing'); // => false
6464

65-
// Need to use the same attributes (path, domain) as what the cookie was written with
65+
// Need to use the same path, domain and secure attributes that were used when writing the cookie
6666
Cookies.set('name', 'value', { path: '/' });
67-
// This won't work!
68-
Cookies.remove('name'); // => false
69-
// This will work!
70-
Cookies.remove('name', { path: '/' }); // => true
67+
Cookies.remove('name'); // fail!
68+
Cookies.remove('name', { path: '/' }); // removed!
7169
```
7270

73-
*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.*
71+
*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).*
7472

7573
## JSON
7674

@@ -102,28 +100,42 @@ Cookies.getJSON('name'); // => { foo: 'bar' }
102100
Cookies.getJSON(); // => { name: { foo: 'bar' } }
103101
```
104102

105-
## RFC 6265
103+
## Encoding
106104

107-
This project assumes [RFC 6265](http://tools.ietf.org/html/rfc6265#section-4.1.1) as a reference for everything. That said, some custom rules are applied in order to provide robustness and cross-browser compatibility.
105+
This project is [RFC 6265](http://tools.ietf.org/html/rfc6265#section-4.1.1) compliant.
106+
However, all special characters that are not allowed in the cookie-value or cookie-name are encoded/decoded with each UTF-8 Hex equivalent. Special characters that consistently work among all supported browsers are not encoded/decoded this way.
108107

109-
### Encoding
110-
All special characters that are not allowed in the cookie-value or cookie-name in at least one supported browser are encoded/decoded with each UTF-8 Hex equivalent. Special characters that consistently work among all supported browsers are not encoded/decoded this way.
108+
## Cookie Attributes
111109

112-
## Cookie Options
113-
114-
Cookie attributes can be set globally by setting properties of the `Cookies.defaults` object or individually for each call to `Cookies.set()` by passing a plain object to the options argument. Per-call options override the default options.
110+
Cookie attributes defaults can be set globally by setting properties of the `Cookies.defaults` object or individually for each call to `Cookies.set(...)` by passing a plain object in the last argument. Per-call attributes override the default attributes.
115111

116112
### expires
117113

118-
expires: 365
114+
Define when the cookie will be removed. Value can be a `Number` which will be interpreted as days from time of creation or a `Date` instance. If omitted, the cookie becomes a session cookie.
115+
116+
**Browser default:** Cookie is removed when the user closes the browser.
117+
118+
**Examples:**
119119

120-
Define lifetime of the cookie. Value can be a `Number` which will be interpreted as days from time of creation or a `Date` object. If omitted, the cookie becomes a session cookie.
120+
```javascript
121+
Cookies.set('name', 'value', { expires: 365 });
122+
Cookies.get('name'); // => 'value'
123+
Cookies.remove('name');
124+
```
121125

122126
### path
123127

124-
path: '/'
128+
Define the path where the cookie is available.
129+
130+
**Browser default:** Path of the page where the cookie was created
131+
132+
**Examples:**
125133

126-
Define the path where the cookie is valid. *By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior).* If you want to make it available for instance across the entire domain use `path: '/'`. Default: path of page where the cookie was created.
134+
```javascript
135+
Cookies.set('name', 'value', { path: '/' });
136+
Cookies.get('name'); // => 'value'
137+
Cookies.remove('name', { path: '/' });
138+
```
127139

128140
**Note regarding Internet Explorer:**
129141

@@ -135,19 +147,34 @@ This means one cannot set a path using `path: window.location.pathname` in case
135147

136148
### domain
137149

138-
domain: 'example.com'
150+
Define the domain where the cookie is available
151+
152+
**Browser default:** Domain of the page where the cookie was created
139153

140-
Define the domain where the cookie is valid. Default: domain of page where the cookie was created.
154+
**Examples:**
155+
156+
```javascript
157+
Cookies.set('name', 'value', { domain: 'sub.domain.com' });
158+
Cookies.get('name'); // => undefined (need to read at 'sub.domain.com')
159+
```
141160

142161
### secure
143162

144-
secure: true
163+
A `Boolean` indicating if the cookie transmission requires a secure protocol (https)
164+
165+
**Browser default:** Doesn't require secure protocol
166+
167+
**Examples:**
145168

146-
If true, the cookie transmission requires a secure protocol (https). Default: `false`.
169+
```javascript
170+
Cookies.set('name', 'value', { secure: true });
171+
Cookies.get('name'); // => 'value' (if already in secure protocol)
172+
Cookies.remove('name', { secure: true });
173+
```
147174

148175
## Converters
149176

150-
Provide a conversion function as optional last argument for reading, in order to change the cookie's value
177+
Provide a conversion function as optional second argument for reading, in order to change the cookie's value
151178
to a different representation on the fly.
152179

153180
Example for parsing the value from a cookie generated with PHP's `setcookie()` method:

0 commit comments

Comments
 (0)