Setting and retrieving cookies in javascript

Here are two function to use cookies directly from javascript. The firt one stock a pair of key/value in the cookie collection. The second one retrieve the value for a key.


function setCookie(idCookie,value)
{
var cookie_date = new Date (); // current date & time
var milli = cookie_date.getMilliseconds()+5*60*1000; // == cokie valid for 5 minutes
cookie_date.setMilliseconds(milli);
cookie_date.setTime ( cookie_date.getTime());
document.cookie = idCookie += "=" + value +"; expires=" + cookie_date.toGMTString();
}

function getCookie ( idCookie )
{

var results = document.cookie.match ( idCookie + '=(.*?)(;|$)' );

if ( results )
return ( unescape ( results[1] ) );
else
return null;
}

No comments: