These two functions will demonstrate how to set the value in cookie and get the value through java script
This function sets the value
function setCookie(){
var name= ‘Mycookie’;
var value =’true’;
var path = ‘/’;var today= new Date();
today.setTime( today.getTime() );//expires in one day
var expires_date = 1 * 1000 * 60 * 60 * 24;var expires = new Date( today.getTime() + (expires_date) );
document.cookie = name + “=” +escape( value ) +
document.cookie = name + “=” +escape( value ) +
( ( expires ) ? “;expires=” + expires.toGMTString() : “” ) +
( ( path ) ? “;path=” + path : “” ) ;}
escape is used for decoding the value
This function retrieves the value form cookie
function getCookie(){
var cookie_values = document.cookie.split( ‘;’ );
var a_temp_cookie = ”;
var cookie_name = ”;
var cookie_value = ”;for ( i = 0; i < cookie_values.length; i++ )
{a_temp_cookie = cookie_values[i].split( ‘=’ );
//trim all white spaces
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, ”);
if ( cookie_name == ‘Mycookie’ )
{
// This handles the case where cookie has no value but exists (no = sign, that is):
if ( a_temp_cookie.length > 1 )
{
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, ”) );
}
// note that in cases where cookie is initialized but no value, null is returned
alert(cookie_value);
break;
}}