Click here to Skip to main content
Click here to Skip to main content

JavaScript Cookie and Query String Wrapper Classes

, 17 Apr 2006
Rate this:
Please Sign up or sign in to vote.
An article on an object oriented approach to moving data to and from client-side JavaScript.


Any web programmer will have good tools to manipulate, read, and write query strings and cookies on the server side. A while back, when trying to utilize this information on the client side, I found the available JavaScript functions cumbersome for retrieving and "storing" multiple pieces of cookie and query string data. They lacked what the server side model has, which is an associative array to retrieve cookie and query string key/value pairs. So I developed the two classes Cookies and QueryString which wrap up the related JavaScript functions and provide you with similar functionality.

Using the code

Instantiate global instances of these classes and initialize them:

window.gCookies = new Cookies();
window.gQueryString = new QueryString();

Retrieving some sample data from these objects:

var iObjId = parseInt(gCookies.GetValue("","myObjId"));
var reportId = gQueryString.GetValue("reportId");

These data structures can store and retrieve data safely until it is later utilized by writing out the cookie to the document (Cookie's Write method), or reassembling the query string (ToString method) and navigating to another page with it.

myQueryString.SetValue("workspaceId", workspaceId);
myLink.href = document.location.pathname + 

The QueryString class is emptied of all key/values by calling the Clear method. The Cookie class does not have a clear Clear method. To destroy a cookie, you must set its value to null, and once Write() is called, it will be forced to expire.

gCookies.SetValue("", "MyIntegerOption", 1);
gCookies.SetValue("", "MyCookieToExpire", null)


  • Article created - 4/17/06.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Derek Petillo
Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pinmemberpaulatwork19-Oct-10 9:45 
GeneralCool PinmemberXmen W.K.27-Jul-09 17:12 
GeneralBrilliant PinmemberDash F28-Jan-09 15:23 
GeneralBug. PinmemberKim Taeyoung25-Jun-08 0:54 
Questionhow to handle when user forbidden cookies? Pinmemberzleeway9-May-06 18:03 
AnswerRe: how to handle when user forbidden cookies? PinmemberDerek Petillo10-May-06 19:09 
GeneralNice Pinmemberjalchr27-Apr-06 0:36 
GeneralRe: Nice PinmemberDerek Petillo27-Apr-06 4:45