Getting and Setting cookies

var foo = $cookie("foo"); // get
$cookie("foo", "bar"); // set

var cookies = $cookie.get("foo", "bar"); // get 2 cookies into an object
$cookie.set({foo: 1, bar: 2}); // set 2 cookies at once

Setting expiration

$cookie("foo", "1"); // expires in 1000 years
$cookie("foo", "1", 5); // expires in 5 seconds
$cookie("foo", "1", "20:00"); // expires in 20 minutes
$cookie("foo", "1", "12:00:00"); // expires in 12 hours
$cookie("foo", "1", "2030-01-01 12:13:30"); // expires at 2030-01-01 12:13:30
$cookie("foo", "1", -1); // expires when the browser is closed
$session("foo", "1"); // same as $cookie("foo", "1", -1);

Setting domain, path

$cookie("foo", "1"); // name: foo, path: (current path), domain: (current domain)
$cookie("/path/foo", "1"); // name: foo, path: /path, domain: (current domain)
$cookie("domain.com/path/foo", "1"); // name: foo, path: /path, domain: domain.com

Etc

var hasFoo = $cookie.has("foo"); // check if there is a cookie named 'foo'
$cookie.remove("foo"); // remove a cookie named 'foo'
$cookie.clear(); // clear all cookies of current domain and current path

Iterating

cookie.js is compatible with from.js. You can iterate cookies of current domain and current path like below:

$from($cookie).each(function(value, name) {
    document.writeln(name + ": " + value);
});

Last edited Jun 8, 2012 at 1:23 AM by suckgamoni, version 6