Click here to Skip to main content
6,597,576 members and growing! (21,082 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

HttpQueryStringBuilder Using JavaScript

By S. M. SOHAN

This is a Utility class for easily creating, modifying and using Http Query Strings from JavaScript
Javascript, Windows, .NET, ASP.NET, Visual Studio, WebForms, Dev
Posted:26 Jun 2007
Views:13,427
Bookmarked:14 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.19 Rating: 3.77 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2

3

4
5 votes, 71.4%
5

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.

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

S. M. SOHAN


Member
I graduated on June, 2007 from Computer Science AND Engineering, BUET (Bangladesh University of Engineering and Technology).
Presently holding a job at Code71,Inc as a Senior Software Engineer.

I am a Scrum Alliance Certified Scrum Master and a Microsoft Certified Technology Specialist.

In future, I hope to take part in the IT decision making process for Bangladesh, my country, and contribute accordingly.

Read my tech blog at http://smsohan.blogspot.com
Occupation: Software Developer (Senior)
Company: Code71, Inc.
Location: Bangladesh Bangladesh

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralDon't use escape(); PinmemberMartin Nemitz23:33 3 Jul '07  
GeneralReinventing the wheel... PinmemberAntonello Provenzano22:53 26 Jun '07  
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jun 2007
Editor:
Copyright 2007 by S. M. SOHAN
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project