5,696,038 members and growing! (13,247 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

JavaScript Cookie and Query String Wrapper Classes

By Derek Petillo

An article on an object oriented approach to moving data to and from client-side JavaScript.
Javascript, Windows, Visual Studio, Dev

Posted: 17 Apr 2006
Updated: 17 Apr 2006
Views: 23,890
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.26 Rating: 3.75 out of 5
1 vote, 25.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 25.0%
4
2 votes, 50.0%
5

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



Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralBug.memberKim Taeyoung0:54 25 Jun '08  
Generalhow to handle when user forbidden cookies?memberzleeway18:03 9 May '06  
GeneralRe: how to handle when user forbidden cookies?memberDerek Petillo19:09 10 May '06  
GeneralNicememberjalchr0:36 27 Apr '06  
GeneralRe: NicememberDerek Petillo4:45 27 Apr '06  
GeneralError in script when I runmembersrini srini19:58 26 Apr '06  
AnswerRe: Error in script when I runmemberDerek Petillo4:55 27 Apr '06  
GeneralRe: Error in script when I runmembersrini srini5:03 27 Apr '06  
GeneralCoolmemberSimonS6:41 25 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Apr 2006
Editor: Smitha Vijayan
Copyright 2006 by Derek Petillo
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project