Email This Post Email This Post Print This Post Print This Post

Simple Set and Get Cookie Scripts

Thu, Nov 12, 2009 – 6:52 am

Since cookies are a common thing to use on the web I thought I would post the function which I use to set and read cookies

//////////////////////////////////////////////////////////////////////////////
// setCookie
//
//		Create a client side cookie.
//
//  @param   c_name - Name to call the cookie
//  @param   value - Value to assign to the cookie
//  @param   expiredays - *optional* number of days before cookie expires
//////////////////////////////////////////////////////////////////////////////
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

//////////////////////////////////////////////////////////////////////////////
// getCookie
//
//		Get value of a client side cookie
//
//  @param   c_name - Cookie name.
//  @return  cookie value, or an empty string if cookie is not found
//////////////////////////////////////////////////////////////////////////////
function getCookie(c_name) {

	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}

	return "";
}

Here is an example of how you would use them

// Set currentView cookie to "View" and set the value to expire in 30 days
setCookie("currentView","View",30);
.
.
.
// Get current view mode from cookie
var myCurrentView = getCookie("currentView");
  1. One Response to “Simple Set and Get Cookie Scripts”

  2. Thanks for the useful things. These are really useful for me…

    By sende tikla on May 9, 2010

Post a Comment