dzLib.js
 

Client-side cookies

Description:
Cookies can be set either client-side or server-side. This mini-lib provides functions for writing and reading cookies, plus a convinience function for setting the expiry-date.

Download: code, view-source.
Targetted: all
Tested on: win32: ie5..ie6, ns6.1, OS X: ie5.1

Usage:

//Test is cookies are enabled
if(testCookie())
{
  //Save a cookie, called 'userName' with value 'Your Name',
  //and let it expire after 2 months.
  setCookie('userName','Your Name',getExpireDate('m',2));
}

//Do the same, but cookie expires when browser (session) is closed.
//The function also returns a boolean value to indicate if the
//cookie was saved successfull.
if( !setCookie('userName','Your Name') )
{
	alert('Saving of the cookie didn\'t succeed');
}

//And show an alert with the value of the cookie 
alert( getCookie('userName') );

Live Demo:

Setting cookies seems to be disabeld. Enable the function and reload the form to run this demo (just enable this freaking form).




(get rid of the cookie, by setting a negative number)