Query String

There are three methods to send data to a server are GET, POST, and PUT, defined in Hypertext Transfer Protocol (HTTP). The GET request appends data to the end of the URL in the form of a list of field=value pair which is known as query string.

The query string is an internet standard format. This a set of characters which is supplied to a Web browser as an input and sent to a query program to get specific information from Server . Query strings generally contain & , ? and = characters.

The following url format conatins the query string in its parameter :


Query String Parameters

Query string parameters are useful on the server side, but it wasn’t until AJAX-driven web apps became popular that we relied too much on them on the client side.

Parameters play a major role outside of the initial page load.

 The query string format is consist of series of name=value pairs. in each pair, the field name and value are separated by an equals sign, ‘=’ and one pair is separated with other page by an . The series of pairs is separated by the ampersand, ‘ & ‘ sign. In the above url there are two parameters in the querystring- page=one and post=3.

There are many different ways are there to retrieve the querystring values (field=value pairs).

The following program shows how to parse a query string and get the values from the request.


function retriveParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));

In the above function, the function retriveParameterByName accept two parameters. The field name and the URL.


The above program will return “one”. If we pass parameter “post” instead of “page” then you will get the result as “3”.

Leave comment

Your email address will not be published. Required fields are marked with *.