/ Published in: JavaScript
Set, get, and erase cookies.
Expand |
Embed | Plain Text
var Cookies = function() { //***** private properties and methods *****// //expires argument can be either a Date object or the number of seconds until it should expire // e.g.: 60: 1 minute from now // (false): end of session // -1: remove cookie function setCookie(name, value, expires, path, domain, secure) { if(!name || !name.toString()) return; value = value.toString() ? escape(value.toString()) : ""; var expiration = ""; if(expires) { //alert(typeof expires); if(expires.getTime) expiration = expires.toGMTString(); //expires on given Date else if(!isNaN(expires)) //expires in given number of seconds { var date = new Date(); date.setTime(date.getTime()+(expires*1000)); expiration = date.toGMTString(); } //else expires at end of session } //else expires at end of session path = path || "/"; domain = domain ? "; domain="+domain : ""; secure = secure ? "; secure" : ""; document.cookie = escape(name)+"="+value+"; expires="+expiration+"; path="+path+domain+secure; } //sets a cookie with an expiration date a number of years into the future function setPersistentCookie(name, value, years, path, domain, secure) { years = years || 50; setCookie(name, value, years*365*24*60*60, path, domain, secure); } function getCookie(name) { if(!name) return null; var nameEQ = escape(name) + "="; var ca = document.cookie.split(";"); var c; for(var i=0; i<ca.length; i++) { c = ca[i]; c = c.replace(/^\s*/, ""); if(c.indexOf(nameEQ) == 0) return unescape(c.slice(nameEQ.length)); } return null; } //Returns an array of cookie values; useful when there is more than one cookie with the same name. //Note that which cookies are returned and in what order depends on the browser, so it's impossible to determine which // instance of a name corresponds to which domain. //If a name is provided, it returns an array of values. //If a name isn't provided, it returns a 2D array of all name/value pairs. function getCookieArray(name) { name = name ? name.toString() : ""; if(!name) { var ca = document.cookie.split(";"); var r = []; for(var i=0; i<ca.length; i++) { ca[i] = ca[i].replace(/\s+/g,""); r[i] = []; r[i][0] = ca[i].replace(/^(.*?)=.*/i, "$1"); r[i][1] = ca[i].replace(/^.*?=(.*)/i, "$1"); } return r; } else { var nameEQ = escape(name) + "="; var ca = document.cookie.split(";"); var c; for(var i=0; i<ca.length; i++) { ca[i] = ca[i].replace(/\s+/g,""); if(ca[i].indexOf(nameEQ) != 0) ca.splice(i--,1); else ca[i] = unescape(ca[i].slice(nameEQ.length)) } return ca; } } function eraseCookie(name, path, domain) { setCookie(name, "", -1, path, domain); } function testSupport() { setCookie("test_cookie_support", "test"); var check = getCookie("test_cookie_support"); eraseCookie("test_cookie_support"); return !!check; } //***** public properties and methods *****// return { set: setCookie, setPersistent: setPersistentCookie, get: getCookie, getArray: getCookieArray, erase: eraseCookie, supported: testSupport() }; }(); //initialize Cookies
Comments
Subscribe to comments
You need to login to post a comment.

Modified to avoid the unneeded closure.
Nevermind--scratch that.
Updated and added Cookies.setPersistent and Cookies.array functions.