Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Javascript
Article

JavaScript Cookie and Query String Wrapper Classes

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
17 Apr 20061 min read 57K   336   20   13
An article on an object oriented approach to moving data to and from client-side JavaScript.

Introduction

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:

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

Retrieving some sample data from these objects:

JavaScript
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.

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

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.

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

History

  • Article created - 4/17/06.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMoved Source Code Pin
Derek Petillo8-Nov-17 11:20
Derek Petillo8-Nov-17 11:20 
GeneralMy vote of 5 Pin
paulatwork19-Oct-10 8:45
paulatwork19-Oct-10 8:45 
GeneralCool Pin
Xmen Real 27-Jul-09 16:12
professional Xmen Real 27-Jul-09 16:12 
GeneralBrilliant Pin
Dash F28-Jan-09 14:23
Dash F28-Jan-09 14:23 
GeneralBug. Pin
Kim Taeyoung24-Jun-08 23:54
Kim Taeyoung24-Jun-08 23:54 
Questionhow to handle when user forbidden cookies? Pin
zleeway9-May-06 17:03
zleeway9-May-06 17:03 
AnswerRe: how to handle when user forbidden cookies? Pin
Derek Petillo10-May-06 18:09
Derek Petillo10-May-06 18:09 
GeneralNice Pin
Eng. Jalal26-Apr-06 23:36
Eng. Jalal26-Apr-06 23:36 
GeneralRe: Nice Pin
Derek Petillo27-Apr-06 3:45
Derek Petillo27-Apr-06 3:45 
GeneralError in script when I run Pin
srini srini26-Apr-06 18:58
srini srini26-Apr-06 18:58 
AnswerRe: Error in script when I run Pin
Derek Petillo27-Apr-06 3:55
Derek Petillo27-Apr-06 3:55 
GeneralRe: Error in script when I run Pin
srini srini27-Apr-06 4:03
srini srini27-Apr-06 4:03 
GeneralCool Pin
SimonS25-Apr-06 5:41
SimonS25-Apr-06 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.