5,702,921 members and growing! (22,048 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, Visual Studio, WebForms, ASP.NET, Dev

Posted: 26 Jun 2007
Updated: 26 Jun 2007
Views: 9,660
Bookmarked: 13 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.85 Rating: 3.67 out of 5
1 vote, 16.7%
1
1 vote, 16.7%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
4 votes, 66.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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


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
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 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralDon't use escape();memberMartin Nemitz23:33 3 Jul '07  
GeneralReinventing the wheel...memberAntonello Provenzano22:53 26 Jun '07  
GeneralRe: Reinventing the wheel...memberS. M. SOHAN1:09 27 Jun '07  
GeneralRe: Reinventing the wheel...memberVasudevan Deepak Kumar5:06 27 Jun '07  
GeneralRe: Reinventing the wheel...memberinetfly1236:22 27 Jun '07  
GeneralRequest.QueryString Library in JavaScriptmemberVasudevan Deepak Kumar22:37 26 Jun '07  
GeneralRe: Request.QueryString Library in JavaScriptmemberS. M. SOHAN1:11 27 Jun '07  
GeneralRe: Request.QueryString Library in JavaScriptmemberVasudevan 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-2008
Web11 | Advertise on the Code Project