Click here to Skip to main content
Licence 
First Posted 17 Apr 2006
Views 39,759
Bookmarked 20 times

JavaScript Cookie and Query String Wrapper Classes

By | 17 Apr 2006 | Article
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:

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

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

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

About the Author

Derek Petillo

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberpaulatwork8:45 19 Oct '10  
GeneralCool PinmemberXmen W.K.16:12 27 Jul '09  
GeneralBrilliant PinmemberDash F14:23 28 Jan '09  
GeneralBug. PinmemberKim Taeyoung23:54 24 Jun '08  
Questionhow to handle when user forbidden cookies? Pinmemberzleeway17:03 9 May '06  
AnswerRe: how to handle when user forbidden cookies? PinmemberDerek Petillo18:09 10 May '06  
GeneralNice Pinmemberjalchr23:36 26 Apr '06  
GeneralRe: Nice PinmemberDerek Petillo3:45 27 Apr '06  
GeneralError in script when I run Pinmembersrini srini18:58 26 Apr '06  
Hi derek,
the script you wrote is excellent for playing with url query strings.
When i try to use this script, i'm getting QueryString is undefined.
below is the snippet i used
 
<script language=javascript>
 
window.gQueryString = new QueryString();
gQueryString.Read();
 
var reportId = gQueryString.GetValue("reportId");
alert(reportId );
myQueryString.Clear();
myQueryString.SetValue("workspaceId", workspaceId);
 

 
one doubt here is that "QueryString" is a predined or user defined class? If we say gQueryString.Read() will it read all query strings and keep in memory so that using "gQueryString.GetValue(...)" we can read any key from that collection?
 
I request you to explain in detail on this topic.
 
thanks,
samudrala
AnswerRe: Error in script when I run PinmemberDerek Petillo3:55 27 Apr '06  
GeneralRe: Error in script when I run Pinmembersrini srini4:03 27 Apr '06  
GeneralCool PinmemberSimonS5:41 25 Apr '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 17 Apr 2006
Article Copyright 2006 by Derek Petillo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid