Cookie Scripting

Est. Reading Time: 3 minutes

Cookies have a range of uses across the web. Cookies are used to identify a visitors (authentication), remembering settings or configuration, or even maintaining a shopping cart or wishlist.  Cookies can store data locally on a visitor’s machine and can be called back as long as the cookie does not expire or get cleaned up by the browser’s Temporary File Cleanup.  On a recent project, we built a ‘Bookmark’ utility as extra functionality into a client’s site.  This functionality required the use of cookies, containing a URL as a reference to the bookmark locations.  jQuery has greatly simplified the scripting for cookies. However, after this previous project I decided to write some re-useable functions to handle common cookie routines.  Packaging multiple values into a cookie can be done through separating the values with a ‘separator’, usually a character the cookie values will not use.  Common separators are the  pipe (|) or tilde(~).  Back to the bookmark example, we can store the URLs in a single cookie with pipe separators as opposed to dropping a new cookie for every bookmark.

Library Resources:

You will need to include the following scripting libraries/resources for methods in the function list below.  You may include the libraries with three individual script tags or use the integrated one compiled below.

  • jQuery – Contains the backbone behind the jQuery library
  • jQuery Cookies – Additional jQuery library for cookies
  • jQuery Functions – Simplified functions for easy cookie project coding

- Integrated JS Library -

Function List Overview

function cookieSet(name,value,exp){
	//set cookie with value
function cookieGet(name){
	//get value of single value cookie
	return $.cookie(name);
function cookiePrint(name){
 //print value of a single cookie
function cookieGlue(name,values,seperator){
 //construct cookie with array of values
	var str = '';
	for(var i=0;i < values.length;i++)
            if(i!=values.length-1) str = str+values[i]+seperator; else str = str+values[i];
function cookieSplit(name,seperator){
 //return array of values in cookie
	var vals = $.cookie(name);
	return vals.split(seperator);
function cookiePrintArray(name,seperator){
 //print cookie array values
	var str = $.cookie(name);
	str = str.split(seperator);
	for(var i=0;i < str.length;i++) document.write(str[i]+' ');
function cookieKill(name){
  //Delete cookie value/array

Cookie Scripting Examples

cookieSet('Name','Thomas Brinegar',7);		//set cookie with 7 day expiration
var cookie = cookieGet('Name');			//assign cookie val into variable
cookiePrint('Name');				//print cookie value

var name = new Array();				//create an array for cookie storage
name[0] = 'Thomas';
name[1] = 'David';
name[2] = 'Brinegar';	

cookieGlue('Name_Array',name,'~');		//glue array values together as cookie
var vals = cookieSplit('Name_Array','~');	//assign cookie values into array
cookiePrintArray('Name_Array','~');		//print array contents

cookieKill('Name');				//remove cookie value
cookieKill('Name_Array');			//remove cookie array