|
/* |
|
* Cookie Manager |
|
* Written in plain 'ole Javascript but based on Klaus Hartl's jQuery cookie plugin |
|
* |
|
* Copyright (c) 2011 Rob Flaherty (@ravelrumba) |
|
* Copyright (c) 2010 Klaus Hartl (stilbuero.de) |
|
* Dual licensed under the MIT and GPL licenses: |
|
* http://www.opensource.org/licenses/mit-license.php |
|
* http://www.gnu.org/licenses/gpl.html |
|
* |
|
* Example Usage: |
|
* |
|
* Get Cookie Value: |
|
* cookie('name'); |
|
* |
|
* Set Session Cookie: |
|
* cookie('name', 'value'); |
|
* |
|
* Set Cookie with options: |
|
* cookie('name', 'value', { expires: 1, path: '/', domain: 'example.com', secure: true }); |
|
* NOTE: Expiration time set in one-hour intervals |
|
* |
|
* Delete Cookie: |
|
* cookie('name', null); |
|
* |
|
*/ |
|
|
|
var cookie = function (key, value, options) { |
|
var expiration, time, result; |
|
|
|
if (arguments.length > 1 && String(value) !== "[object Object]") { |
|
options = options || {}; |
|
|
|
if (value === null || value === undefined) { |
|
options.expires = -1; |
|
} |
|
|
|
if (typeof options.expires === 'number') { |
|
expiration = options.expires; |
|
time = options.expires = new Date(); |
|
time.setTime(time.getTime() + expiration * 60 * 60 * 1000); |
|
} |
|
|
|
return (document.cookie = [ |
|
encodeURIComponent(key), '=', |
|
encodeURIComponent(value), |
|
options.expires ? '; expires=' + options.expires.toUTCString() : '', |
|
options.path ? '; path=' + options.path : '', |
|
options.domain ? '; domain=' + options.domain : '', |
|
options.secure ? '; secure' : '' |
|
].join('')); |
|
} |
|
|
|
result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie); |
|
return result ? decodeURIComponent(result[1]) : null; |
|
}; |