Instantly share code, notes, and snippets.
Created
May 6, 2010 14:10
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save felipelavinz/392162 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cookieHelper = { | |
/** | |
* Create a new cookie and add it to the document | |
* @argument name {string} The "key" name of the cookie | |
* @argument value {string} The "value" of the cookie | |
* @argument days {number} The number of days this cookie should be kept | |
* @author Peter-Paul Koch <quirksmode.org> | |
*/ | |
create : function(name,value,days) { | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime()+(days*24*60*60*1000)); | |
var expires = "; expires="+date.toGMTString(); | |
} | |
else var expires = ""; | |
document.cookie = name+"="+value+expires+"; path=/"; | |
}, | |
/** | |
* Read a cookie from this domain | |
* @argument name {string} The "key" name of the cookie | |
* @author Peter-Paul Koch <quirksmode.org> | |
*/ | |
read : function(name) { | |
var nameEQ = name + "="; | |
var ca = document.cookie.split(';'); | |
for(var i=0;i < ca.length;i++) { | |
var c = ca[i]; | |
while (c.charAt(0)==' ') c = c.substring(1,c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); | |
} | |
return null; | |
}, | |
/** | |
* Delete a previously set cookie | |
* @argument name {string} The "key" name of the cookie to be erased | |
* @author Peter-Paul Koch <quirksmode.org> | |
*/ | |
erase : function(name) { | |
cookieHelper.createCookie(name,"",-1); | |
}, | |
/** | |
* Modifiy a previously set cookie. Previous info will be lost | |
* @argument name {string} The "key" name of the cookie to be modified | |
* @argument newval {string|object} The new "value" to be added or to replace previous value | |
* @author Felipe Lavin <yukei.net> | |
*/ | |
modify : function(name, newval){ | |
overwrite = ( overwrite === true ) ? true : false; | |
_prevCookie = cookieHelper.read('name'); | |
if ( typeof(_prevCookie) === 'string' ) { | |
value = cookieHelper.decodeValue(_prevCookie); | |
if ( typeof(newval) === 'object' ) { | |
cookieHelper.create(name, cookieHelper.encodeValue(newval)); | |
} else if ( typeof(newval) === 'string' ) { | |
cookieHelper.create(name, newval); | |
} | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
/** | |
* Decode a query string-like value into an object | |
* Modified from Thickbox | |
* @argument value {string} The "value" from the cookie | |
* @returns {object|false} An object on success, false on failure | |
* @author Cody Lindley <codylindley.com> | |
*/ | |
decodeValue : function(value) { | |
var Params = {}; | |
if ( !value ) {return false;}// return false | |
var Pairs = value.split(/[;&]/); | |
_pairsLength = Pairs.length; | |
if ( typeof(Pairs) === 'object' && _pairsLength ) { | |
for ( var i = 0; i < _pairsLength; i++ ) { | |
var KeyVal = Pairs[i].split('='); | |
if ( ! KeyVal || KeyVal.length != 2 ) {continue;} | |
var key = unescape( KeyVal[0] ); | |
var val = unescape( KeyVal[1] ); | |
val = val.replace(/\+/g, ' '); | |
Params[key] = val; | |
} | |
return Params; | |
} else { | |
return false; | |
} | |
}, | |
/** | |
* Encode an object into a query-string-like string | |
* @argument value {object} The object to be parametized | |
* @returns {string} Parametized string | |
* @uses {function} $.param() | |
* @author Felipe Lavín <yukei.net> | |
*/ | |
encodeValue : function(value) { | |
if ( typeof(value) === 'object' ) { | |
return $.param(value); | |
} else { | |
return false; | |
} | |
}, | |
/** | |
* Get an object with the cookies associated with the current document | |
* @returns {object} Object representation of the document's cookies | |
* @author Chris Hope <electrictoolbox.com> | |
*/ | |
getCookies : function() { | |
var cookies = new Object(); | |
if (document.cookie && document.cookie != '') { | |
var split = document.cookie.split(';'); | |
for (var i = 0; i < split.length; i++) { | |
var name_value = split[i].split("="); | |
name_value[0] = name_value[0].replace(/^ /, ''); | |
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]); | |
} | |
} | |
return cookies; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment