Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralCool Pin
Xmen W.K.
17:12 27 Jul '09  
GeneralBrilliant Pin
Dash F
15:23 28 Jan '09  
GeneralBug. Pin
Kim Taeyoung
0:54 25 Jun '08  
Generalhow to handle when user forbidden cookies? Pin
zleeway
18:03 9 May '06  
GeneralRe: how to handle when user forbidden cookies? Pin
Derek Petillo
19:09 10 May '06  
GeneralNice Pin
jalchr
0:36 27 Apr '06  
GeneralRe: Nice Pin
Derek Petillo
4:45 27 Apr '06  
GeneralError in script when I run Pin
srini srini
19:58 26 Apr '06  
AnswerRe: Error in script when I run Pin
Derek Petillo
4:55 27 Apr '06  
GeneralRe: Error in script when I run Pin
srini srini
5:03 27 Apr '06  
GeneralCool Pin
SimonS
6:41 25 Apr '06  


Last Updated 17 Apr 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009