Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / Javascript

HttpQueryStringBuilder Using JavaScript

Rate me:
Please Sign up or sign in to vote.
3.77/5 (7 votes)
26 Jun 2007CPOL 34.4K   17   8
This is a utility class for easily creating, modifying, and using HTTP querystrings from JavaScript.

Introduction

This code may be considered as a utility tool for creating, modifying, accessing HTTP querystrings with GET/POST, very easily using JavaScript from the client side.

Background

Nowadays, when working with raw AJAX, we need to create and manipulate the query strings at the client side using JavaScript and send it to the server. Sometimes, specially when relatively large data are transmitted in querystrings, it may get painful to modify one of the data items once added to the list, and so on.

To help this task, this utility should be very helpful to its users. Also, it is simple to use as it is developed using object oriented JavaScript code.

Using the code

The code contains two segments:

  1. The utility routine.
  2. The Test method demonstrating how to make use of the utility routine.
JavaScript
// The class for Creating HTTPQueryString
function HttpQueryStringBuilder()
{
    //Holds the Url
    this.Url = '';    
    //Holds the Array of Key Value Pairs
    this.Pairs = new Array();    
    //The method for getting the final query string
    HttpQueryStringBuilder.prototype.GetFullString = function()
    {
        var queryString = (this.Url.length > 0) ? this.Url + "?" : '';
        for(var key in this.Pairs)
        {
            queryString += escape(key) + "=" + escape(this.Pairs[key]) + "&";
        }
        return queryString.substring(0, queryString.length - 1);
    }
}

////////////////////////////////////////
//
// The Test() Method is added for demonstration purpose only
// Delete this method when you are done with testing
//
////////////////////////////////////////
function Test()
{
    //Define the Object
    var builder = new HttpQueryStringBuilder();
    
    //Supply values
    builder.Url = "http://www.google.com"
    //Pairs[Key] = value (Dont worry about url encoding, it will be handled automatically)
    builder.Pairs["FirstName"] = "S M";
    builder.Pairs["LastName"] = "Sohan";
    builder.Pairs["EMail"] = "sohan39@gmail.com";
    
    //Done with insertions! show it! 
    alert(builder.GetFullString());    
    
    //Make some changes
    builder.Pairs["FirstName"] = "Sheikh Mohammad";
    builder.Pairs["EMail"] = "sohan39@yahoo.com";
    
    //Done with modifications! show it again! 
    alert(builder.GetFullString());    
}

To use the supplied test code, you need a markup like this in one of your pages:

HTML
<script type="text/javascript" src="HttpQueryStringBuilder.js"></script>
<input type="button" value="Button" onClick="Test()" />

History

  • June 30, 2007: Edited for the first time, and submitted as unedited post to CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other ThoughtWorks
Canada Canada
Consultant
Read my blog at http://smsohan.blogspot.com

Comments and Discussions

 
GeneralDon't use escape(); Pin
Martin Nemitz3-Jul-07 22:33
Martin Nemitz3-Jul-07 22:33 
GeneralReinventing the wheel... Pin
Antonello Provenzano26-Jun-07 21:53
Antonello Provenzano26-Jun-07 21:53 
GeneralRe: Reinventing the wheel... Pin
S. M. SOHAN27-Jun-07 0:09
S. M. SOHAN27-Jun-07 0:09 
GeneralRe: Reinventing the wheel... Pin
Vasudevan Deepak Kumar27-Jun-07 4:06
Vasudevan Deepak Kumar27-Jun-07 4:06 
GeneralRe: Reinventing the wheel... Pin
ryanoc33327-Jun-07 5:22
ryanoc33327-Jun-07 5:22 
GeneralRequest.QueryString Library in JavaScript Pin
Vasudevan Deepak Kumar26-Jun-07 21:37
Vasudevan Deepak Kumar26-Jun-07 21:37 
There is already a Request.QueryString library in Javascript from this website http://andrewu.co.uk/tools/request/[^]

I also have a review on it over here: http://www.dotnetspider.com/kb/Article3196.aspx[^]

Vasudevan Deepak Kumar
Personal Homepage
Tech Gossips

GeneralRe: Request.QueryString Library in JavaScript Pin
S. M. SOHAN27-Jun-07 0:11
S. M. SOHAN27-Jun-07 0:11 
GeneralRe: Request.QueryString Library in JavaScript Pin
Vasudevan Deepak Kumar27-Jun-07 4:05
Vasudevan Deepak Kumar27-Jun-07 4:05 

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.