JavaScript : Smart Sites and Cute Cookies
Dr Benton
Function for Updating a Cookie
The following function will update the data saved in an existing cookie each time a visitor returns to your site:
function miseAJour(nom)
{
var maintenant = new Date();
var expiration = new Date();
// Expiry date is in 365 days:
expiration.setTime(maintenant.getTime() + 1000*60*60*24*365);
// Writing the cookie:
ecritCookie(nomCookie, nom, expiration);
}
The function
miseAJour() expects the parameter
nom which corresponds to the visitor's name. It recalculates the cookie expiry date, then calls up the function
ecritCookie() giving it the generic name defined for the cookie, the visitor's name and the new expiry date, set at 365 days from now.
If the visitor does not return with one year, the cookie will be deleted.
That's it as far as handling cookies is concerned. So let's take a look at the HTML code.