Skip to content

Commit

Permalink
Changed: Better markup syntax for examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonlabelle committed Sep 19, 2012
1 parent d964fd8 commit 6d2ddb9
Showing 1 changed file with 145 additions and 87 deletions.
232 changes: 145 additions & 87 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,178 +3,236 @@ Cookie.js


Manage all your cookies and sub-cookies with Cookie.js. Cookie.js is a stand-alone script and does NOT require additional libraries (jQuery, YUI, etc.) to operate. Manage all your cookies and sub-cookies with Cookie.js. Cookie.js is a stand-alone script and does NOT require additional libraries (jQuery, YUI, etc.) to operate.



Usage Usage
----- -----


`<script type="text/javascript" src="Cookie.min.js"></script>` Add [Cookie.min.js](https://raw.github.com/jonlabelle/cookie-js/master/Cookie.min.js) to your HTML document.


```html
<script type="text/javascript" src="Cookie.min.js"></script>
```


Creating cookies Cookie.set()
---------------- ------------

Create a cookie using the default options


### Create a cookie with default options ```javascript
Cookie.set("firstname", "jon");
```


`Cookie.set("firstname", "jon");` You can also set multiple cookies at once (chaining)


Adds a cookie named *firstname* with a value of *jon*, that expires when the session ends. ```javascript
Cookie.set("firstname", "jon").set("id", "1234");
```


### Set the expires option #### Set the *expires* option


`Cookie.set("firstname", "jon", { ```javascript
Cookie.set("firstname", "jon", {
expires: new Date("March 18, 2040") expires: new Date("March 18, 2040")
});` });
```


`Cookie.set("firstname", "jon", { ```javascript
Cookie.set("firstname", "jon", {
expires: 30 // expires in 30-days expires: 30 // expires in 30-days
});` });
```


The `expires` option can be a `Date` or a `Number` value. The `expires` option can be a `Date` or a `Number` value type. If the `expires` option is NOT set, it will be a *session* cookie (expiring when the session ends).


### Set the path and domain options #### Set the *path* and *domain* options


`Cookie.set("firstname", "jon", { ```javascript
Cookie.set("firstname", "jon", {
path: "/", path: "/",
domain: ".jonlabelle.com" domain: ".jonlabelle.com"
});` });
```


### Set the secure option #### Set the *secure* option


`Cookie.set("secure_name", "jon", { ```javascript
Cookie.set("secure_name", "jon", {
secure: true secure: true
});` });
```


Setting the `secure` option to `true` ensures the cookie is always encrypted when transmitting from client to server. Setting the `secure` option to `true` ensures the cookie is always encrypted when transmitting from client to server.


Cookie.get()
------------


Reading cookies ```javascript
--------------- Cookie.get("firstname");

```
`Cookie.get("firstname");`


Returns `null` if the cookie does NOT exist. Returns `null` if the cookie does NOT exist.


### Cookie exists #### Cookie exists


`if (Cookie.exists("firstname")) { ```javascript
if (Cookie.exists("firstname")) {
console.log('firstname cookie exists!'); console.log('firstname cookie exists!');
}` }
```


Returns `bool`, `true` if exists, `false` if the cookie does not exist. Returns `bool`, `true` if exists, `false` if the cookie does not exist.


### Read cookie and type #### Read cookie and type

The returned cookie value will be a number if the cookie exists (it will still be null if the cookie doesn't exist). Other native functions that convert values are Boolean() and Date, or you can define your own conversion `function`.


`var value = Cookie.get("age", Number);` The returned cookie value will be a number if the cookie exists (it will still be null if the cookie doesn't exist). Other native functions that convert values are `Boolean` and `Date`, or you can define your own conversion `function`.


`console.log(value); // outputs 34 ` ```javascript
Cookie.set("age", 34);


Custom conversion function: var value = Cookie.get("age", Number);


`var value = Cookie.get("code", function (stringValue) { if (typeof (value) === "number") {
return parseInt(stringValue, 16); // create a number from a hexadecimal code console.log(value); // outputs 34
});` }
```


Using a custom conversion function:


Deleting cookies ```javascript
---------------- var value = Cookie.get("code", function (stringValue) {
return parseInt(stringValue, 16); // create a number from a hexadecimal code
});
```


### Delete the cookie named *code*. Cookie.remove()
---------------


`Cookie.remove("code");` Delete the cookie named *code*


### Delete the cookie named *info* on the *jonlabelle.com* domain ```javascript
Cookie.remove("code");
```


`Cookie.remove("info", { Delete the cookie named *info* on the *jonlabelle.com* domain

```javascript
Cookie.remove("info", {
domain: "jonlabelle.com" domain: "jonlabelle.com"
});` });
```


### Delete the secure cookie named *username* Delete a *secure* cookie


`Cookie.remove("username", { ```javascript
Cookie.remove("username", {
secure: true secure: true
});` });
```


### Delete multiple cookies at the same time Delete multiple cookies at the same time


`Cookie.remove("username").remove("ident");` ```javascript
Cookie.remove("username").remove("ident");
```


Cookie.setSub()
---------------


Creating sub-cookies ```javascript
-------------------- Cookie.setSub("ident", "firstname", "jon");
```


`Cookie.setSub("ident", "firstname", "jon");` ```javascript
Cookie.setSub("ident", "lastname", "labelle");
```


`Cookie.setSub("ident", "lastname", "labelle");` #### Set sub-cookie options

### Setting sub-cookie options


`Cookie.setSub("ident", "age", 22, { ```javascript
Cookie.setSub("ident", "age", 22, {
domain: "jonlabelle.com" domain: "jonlabelle.com"
});` });
```


`Cookie.setSub("ident", "firstname", "ace123", { ```javascript
Cookie.setSub("ident", "firstname", "ace123", {
secure: true secure: true
});` });
```


### Setting sub-cookie options with an object #### Set sub-cookie values with an *object*


`var subCookieData = { ```javascript
var subCookieData = {
firstname: "jon", firstname: "jon",
lastname: 'labelle', lastname: 'labelle',
age: 34, age: 34,
ip: ::1 ip: ::1
};` };

```
`Cookie.setSubs("ident", subCookieData);`

*IMPORTANT*: Calls to `setSubs()` will always completely overwrite the cookie.


```javascript
Cookie.setSubs("ident", subCookieData);
```


Reading sub-cookies NOTE: Calls to `Cookie.setSubs()` will always completely overwrite the cookie.
-------------------


`var fname = Cookie.getSub("indent", "firstname");` Cookie.getSubs()
----------------


### Get a sub-cookie and convert to number ```javascript
Cookie.getSub("indent", "firstname");
```


`var nValue = Cookie.getSub("ident", "age", Number);` Get a sub-cookie value and convert it to `Number` value type.


### Get all sub-cookie data ```javascript
Cookie.getSub("ident", "age", Number);
```


`var subCookieData = Cookie.getSubs("indent");` Get sub-cookie data as object


`var subCookieDataVal = subCookieData.subname;` ```javascript
var obj = Cookie.getSubs("indent");


if (typeof (obj) === "object") {
console.log(obj);
}
```


Deleting sub-cookies Cookie.removeSub()
-------------------- ------------------


`Cookie.setSub("ident", "age", 22, { ```javascript
// set it...
Cookie.setSub("ident", "age", 22, {
domain: "jonlabelle.com" domain: "jonlabelle.com"
});` });


`Cookie.removeSub("ident", "age", { // remove it
Cookie.removeSub("ident", "age", {
domain: "jonlabelle.com" domain: "jonlabelle.com"
});` });

```
### Remove all data in sub-cookie


`Cookie.remove("ident");` Remove all data in sub-cookie


```javascript
Cookie.remove("ident");
```


Cookie support Cookie.enabled()
-------------- ----------------


### Check if cookies are enabled by the browser. Check if cookies are enabled by the browser.


`Cookie.enabled();` ```javascript
Cookie.enabled();
```


Returns `bool`, `true` if cookies are enabled, `false` if they are disabled. Returns `bool`, `true` if cookies are enabled, `false` if they are disabled.



Feedback Feedback
-------- --------


[Jon LaBelle](http://jonlabelle.com) Jon LaBelle
<contact@jonlabelle.com>

0 comments on commit 6d2ddb9

Please sign in to comment.