Skip to content

Commit

Permalink
Set Cookies.defaults.path to '/', and updated documentation to reflec…
Browse files Browse the repository at this point in the history
…t the change. Also added a note about encoding to the readme.
  • Loading branch information
ScottHamper committed Apr 28, 2012
1 parent 036cd76 commit 5920313
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ versions:
It is recommended to use Douglas Crockford's [json2.js](https://github.com/douglascrockford/JSON-js) or Kit Cambridge's [json3.js](http://bestiejs.github.com/json3/)
library for a `JSON` shim.

## A Note About Encoding

Cookies.js URI encodes cookie keys and values, and expects cookie keys to be URI encoded when accessing a cookie. In addition,
before the cookie value is URI encoded, it is first JSON encoded via `JSON.stringify`. Keep this in mind when working with cookies on
the server side.

By URI encoding the cookie key, more types of characters can be used. However, it can be tricky to deal with this on the server side.
For example, when using `encodeURIComponent`, escaped sequences are capitalized (e.g., `%3A`), but when using C#'s `HttpUtility.UrlEncode`
function, escaped sequences are lower cased (e.g., `%3a`). These are treated as two separate cookie keys. Due to complications like this,
it is recomended not to use special characters in cookie keys.

# API Reference

Expand Down Expand Up @@ -139,13 +149,14 @@ A boolean value of whether or not the browser has cookies enabled.

### Cookies.defaults
An object representing default options to be used when setting and expiring cookie values.
By default, `Cookies.defaults` is an empty object, but supports the following properties:
`Cookies.defaults` supports the following properties:

*path*: A string value of the path of the cookie
*domain*: A string value of the domain of the cookie
*expires*: A number (of seconds), a date parsable string, or a `Date` object of when the cookie will expire
*secure*: A boolean value of whether or not the cookie should only be available over SSL

By default, only `Cookies.defaults.path` is set to `'/'`, all other properties are `undefined`.
If any property is left undefined, the browser's default value will be used instead.

#### Example Usage:
Expand Down
4 changes: 3 additions & 1 deletion cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
return Cookies._cache[key];
};

Cookies.defaults = {};
Cookies.defaults = {
path: '/'
};

Cookies.set = function (key, value, options) {
var options = {
Expand Down
6 changes: 3 additions & 3 deletions cookies.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/src/cookies.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5920313

Please sign in to comment.