Click here to Skip to main content
Licence CPOL
First Posted 26 Jun 2007
Views 18,754
Bookmarked 16 times

HttpQueryStringBuilder Using JavaScript

By S. M. SOHAN | 26 Jun 2007
This is a utility class for easily creating, modifying, and using HTTP querystrings from JavaScript.
1 vote, 14.3%
1
1 vote, 14.3%
2

3

4
5 votes, 71.4%
5
3.77/5 - 7 votes
μ 3.77, σa 3.03 [?]

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

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

About the Author

S. M. SOHAN

Other
ThoughtWorks
Canada Canada

Member

Follow on Twitter Follow on Twitter
Consultant
Read my blog at http://smsohan.blogspot.com

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
GeneralDon't use escape(); PinmemberMartin Nemitz23:33 3 Jul '07  
GeneralReinventing the wheel... PinmemberAntonello Provenzano22:53 26 Jun '07  
In AJAX frameworks /where more logically you should use a query-string formatter) this exists from the beginning: in prototype (http://www.prototypejs.org) for example, you can convert an Hash (eg. ["name" : "Antonello", "surname" : "Provenzano ]) into a query-string by calling ".toQueryString" function (eg. "name=Antonello&surname=Provenzano"), without pain.
GeneralRe: Reinventing the wheel... PinmemberS. M. SOHAN1:09 27 Jun '07  
GeneralRe: Reinventing the wheel... PinmemberVasudevan Deepak Kumar5:06 27 Jun '07  
GeneralRe: Reinventing the wheel... Pinmemberinetfly1236:22 27 Jun '07  
GeneralRequest.QueryString Library in JavaScript PinmemberVasudevan Deepak Kumar22:37 26 Jun '07  
GeneralRe: Request.QueryString Library in JavaScript PinmemberS. M. SOHAN1:11 27 Jun '07  
GeneralRe: Request.QueryString Library in JavaScript PinmemberVasudevan Deepak Kumar5:05 27 Jun '07  

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
Web02 | 2.5.120210.1 | Last Updated 27 Jun 2007
Article Copyright 2007 by S. M. SOHAN
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid