Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This code may be considered as a utility tool to its users for creating, modifying, accessing http query strings with GET/POST very easily using JavaScript from client side.

Background

Now a days, 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 query string it may get painful to modify one of the data once added to the list and so on.

To help this task, this utility should be very helpful to its users. Also its 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 may have 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, Edited for the first time and submitted as unedited post to CodeProject.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDon't use escape();
Martin Nemitz
23:33 3 Jul '07  
You should use encodeURIComponent instead.

I have not tested it yet, but as far as I know escape() (which should be replace with encodeURI()) won't escape '&'.

So the following code should mess up.
//Make some changes
builder.Pairs["Name"] = "Peter, Paul & Mary";
builder.Pairs["EMail"] = "someemail@mydomain.com";

//Done with modifications! show it again!
alert(builder.GetFullString());

Greeting
Martin

GeneralReinventing the wheel...
Antonello Provenzano
22: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...
S. M. SOHAN
1:09 27 Jun '07  
Hi,
Thanks for your reply. I visited the link. I think, I tried to present a simple copy-paste solution to the problem instead of a whole library.

Hope someone looking for such quick solution would get helps from this article.

Had you not been blessed with your eyes, you would miss the colors of life. So, donate posthumous eye and bring color to the lives of the deprived friends.

GeneralRe: Reinventing the wheel...
Vasudevan Deepak Kumar
5:06 27 Jun '07  
S. M. SOHAN wrote:
I tried to present a simple copy-paste solution to the problem instead of a whole library.

I agree to this statement. Smile

Vasudevan Deepak Kumar
Personal Homepage Tech Gossips

GeneralRe: Reinventing the wheel...
inetfly123
6:22 27 Jun '07  
Me 2, thanks for the "simple" solution. It seems like some people like to complicate code to make it look more sophisticated.
GeneralRequest.QueryString Library in JavaScript
Vasudevan Deepak Kumar
22:37 26 Jun '07  
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
S. M. SOHAN
1:11 27 Jun '07  
I think your link shows nice way to retrieve the values from query string. I posted this article to aid the creation and manipulation of the query string instead of reading from the query string.

Anyway, thank you for your reply.


Had you not been blessed with your eyes, you would miss the colors of life. So, donate posthumous eye and bring color to the lives of the deprived friends.

GeneralRe: Request.QueryString Library in JavaScript
Vasudevan Deepak Kumar
5:05 27 Jun '07  
GETtor and SETtor tools would complement each other in thier efforts. Smile

Vasudevan Deepak Kumar
Personal Homepage Tech Gossips


Last Updated 27 Jun 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010