Skip to content

Commit b94993a

Browse files
Enhances README documentation
Remove "Cookie Options" concept and instead consistently use the "Cookie Attributes" concept. My theory is that people think that "options" are customized to the more convenient defaults, while attributes make it clear they are just passed through to the cookie attributes in the browser. Add the default browser behavior in bold right below each attribute, along with an example of how to create, read and remove.
1 parent 2847ee7 commit b94993a

1 file changed

Lines changed: 51 additions & 17 deletions

File tree

‎README.md‎

Lines changed: 51 additions & 17 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,18 @@ 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+
// IMPORTANT!
66+
// Need to use the same path, domain and secure attributes that were used when writing the cookie
6667
Cookies.set('name', 'value', { path: '/' });
68+
6769
// This won't work!
6870
Cookies.remove('name'); // => false
71+
6972
// This will work!
7073
Cookies.remove('name', { path: '/' }); // => true
7174
```
7275

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.*
76+
*Note: 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).*
7477

7578
## JSON
7679

@@ -109,21 +112,37 @@ This project assumes [RFC 6265](http://tools.ietf.org/html/rfc6265#section-4.1.1
109112
### Encoding
110113
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.
111114

112-
## Cookie Options
115+
## Cookie Attributes
113116

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.
117+
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.
115118

116119
### expires
117120

118-
expires: 365
121+
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.
122+
123+
**Browser default:** Removed when the user closes the browser.
119124

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.
125+
**Examples:**
126+
127+
```javascript
128+
Cookies.set('name', 'value', { expires: 365 });
129+
Cookies.get('name'); // => 'value'
130+
Cookies.remove('name');
131+
```
121132

122133
### path
123134

124-
path: '/'
135+
Define the path where the cookie is available.
136+
137+
**Browser default:** Path of the page where the cookie was created
125138

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.
139+
**Examples:**
140+
141+
```javascript
142+
Cookies.set('name', 'value', { path: '/' });
143+
Cookies.get('name'); // => 'value'
144+
Cookies.remove('name', { path: '/' });
145+
```
127146

128147
**Note regarding Internet Explorer:**
129148

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

136155
### domain
137156

138-
domain: 'example.com'
157+
Define the domain where the cookie is available
158+
159+
**Browser default:** Domain of the page where the cookie was created
139160

140-
Define the domain where the cookie is valid. Default: domain of page where the cookie was created.
161+
**Examples:**
162+
163+
```javascript
164+
Cookies.set('name', 'value', { domain: 'sub.domain.com' });
165+
Cookies.get('name'); // => undefined (need to read at 'sub.domain.com')
166+
```
141167

142168
### secure
143169

144-
secure: true
170+
A `Boolean` indicating if the cookie transmission requires a secure protocol (https)
171+
172+
**Browser default:** Doesn't require secure protocol
173+
174+
**Examples:**
145175

146-
If true, the cookie transmission requires a secure protocol (https). Default: `false`.
176+
```javascript
177+
Cookies.set('name', 'value', { secure: true });
178+
Cookies.get('name'); // => 'value' (if already in secure protocol)
179+
Cookies.remove('name', { secure: true });
180+
```
147181

148182
## Converters
149183

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

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

0 commit comments

Comments
 (0)