CookieManager.js # Cookies management Javascript tool library.
28/01/2009
/** * Cookies management Javascript tool library. * FileName: cookiemanager.js * Date: 15/11/2006 * Author: Gabriel DROMARD */ /** * Retreive a cookie. * @param name The cookie name. */ var getCookie = function(name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var endstr = document.cookie.indexOf (";", j); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(j, endstr)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; }; /** * Store a cookie. * @param name Cookie name * @param value Cookie value * @param expires (optional) the date on which the cookie will expire (default is current date + 300 days) * @param path (optional) the cookie path * @param domain (optional) the cookie domain * @param secure (optional) Is the cookie secure ? (default value: false) */ var setCookie = function(name, value) { //alert('setCookie('+name+' = '+value+')'); var expDays = 300; var exp = new Date(); exp.setTime(exp.getTime() + (expDays*24*60*60*1000)); var argv = setCookie.arguments; var argc = setCookie.arguments.length; var expires = (argc > 2) ? argv[2] : exp; var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); }; /** * Delete an existing cookie. * @param the cookie name. */ var deleteCookie = function (name) { var exp = new Date(); exp.setTime (exp.getTime() - 1); var cval = getCookie (name); if(cval != undefined) document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); };