//sparkl javascript library //February 2008 //original code Neil J Anderson //rubbish about: http://neilanderson.freehostia.com/ //version 0.1 //Cookie's module /* DOCUMENTATION General cookie handling. Doesn't require anything from the core so it can declare the sparkl namespace nja - 2008-02-06 */ /* TO DO / REVISION HISTORY * Allow users to pass any number of args to the bake function */ //COOKIE MODULE //cookie handling functions /* MODULE sparkl.cookie PROPERTIES * has -- Boolean to store whether cookies are available Set by an anonymous function onload DEFAULT true * root -- Boolean use '/' as path DEFAULT true METHODS * bake -- write a cookie * read -- read a cookie * kill -- delete a cookie */ //namespace var sparkl = sparkl || {}; sparkl.cookies = {};//cookie handling //PROPERTIES //cookie availablity sparkl.cookies.has = true; //assume yes sparkl.cookies.root = true; //assume yes //METHODS //bake cookie sparkl.cookies.bake = function(name, value, age, path, domain, secure){ if(sparkl.cookies.has == false) return; if(age){ var ageStr = '; expires='; if(typeof age == 'string'){ var x = parseInt(age); var t = age.match(/([d,m,y])/g).toString(); var dtm = new Date(); switch(t){ case 'd': dtm.setDate(dtm.getDate() + x); break; case 'm': dtm.setMonth(dtm.getMonth() + x); break; case 'y': dtm.setFullYear(dtm.getFullYear() + x); }; ageStr += dtm.toUTCString(); } else{ ageStr += age.toUTCString(); }; } if(!path && sparkl.cookies.root) path = '/'; var cookieString = name + '=' + encodeURIComponent(value) + ((ageStr) ? ageStr : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure=' : ''); document.cookie = cookieString; }; //read cookie sparkl.cookies.read = function(name){ if (document.cookie == '') return false; var at = document.cookie.indexOf(name + '='); if (at != -1){ var start = at + name.length + 1; if (document.cookie.indexOf(';', start) != -1){ var end = document.cookie.indexOf(';', start); return decodeURIComponent(document.cookie.substring(start, end)); } else{ return decodeURIComponent(document.cookie.substring(start)); }; }; }; //delete cookie sparkl.cookies.kill = function(name){ sparkl.cookies.bake(name, '', -1); }; //SET COOKIE AVAILABILITY (function(){ sparkl.cookies.bake('test', 'on'); if (sparkl.cookies.read('test')!='on'){ sparkl.cookies.has = false; } else{ sparkl.cookies.has = true; }; })();