|
### |
|
jQuery.cookie: works with cookies (surprisingly). |
|
Original: lost somewhere across the vast expanses of the Internet. |
|
|
|
Usage: |
|
//Retrieve cookie value |
|
var cookieval = jQuery.cookie('some_cookie_name'); |
|
|
|
//Set cookie |
|
jQuery.cookie('some_cookie_name', 'someval'); |
|
|
|
//Set cookie with options |
|
jQuery.cookie('some_cookie_name', 'someval', {path: '/somepath', expires: 1, secure: true}); |
|
|
|
//Expire(i.e. remove) cookie. Works only with cookies created with 'expires' option. |
|
jQuery.cookie('some_cookie_name', null); |
|
|
|
Have fun. |
|
### |
|
|
|
(($J) -> |
|
$J.cookie = (args...) -> |
|
[key, val, opts] = args |
|
|
|
if args.length > 1 and (val is null or typeof val isnt "object") |
|
opts = jQuery.extend {}, opts |
|
opts.expires = -1 if val is null |
|
if typeof opts.expires is "number" |
|
h = opts.expires |
|
e = opts.expires = new Date |
|
e.setDate e.getDate() + h |
|
return document.cookie = [ |
|
encodeURIComponent(key), "=", |
|
(if opts.raw then String(val) else encodeURIComponent(String(val))), |
|
(if opts.expires then "; expires=" + opts.expires.toUTCString() else ""), |
|
(if opts.path then "; path=" + opts.path else ""), |
|
(if opts.domain then "; domain=" + opts.domain else ""), |
|
(if opts.secure then "; secure" else "") |
|
].join("") |
|
|
|
opts = val or {} |
|
e = (if opts.raw then (b) -> b else decodeURIComponent) |
|
|
|
(if (h = RegExp("(?:^|; )" + encodeURIComponent(key) + "=([^;]*)").exec(document.cookie)) then e(h[1]) else null) |
|
)(jQuery) |