Best Jquery ,javascript, HTML5 CSS3 tutorial with jquery examples, php script , demos for beginners, web designers and web developers.

Jquery

Jquery Training

Jquery training blog

Get query string parameters with javascript

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. Continue Reading


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 :

http://jquerytraining.com?page=one&post=3

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.

retriveParameterByName('page','http://jquerytraining.com?page=one&post=3');

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

Please follow and like us:






About the author

Joydeb Choudhury holds a Master's degree in Computer Application from I.G.N.O.U. After working as a web developer in various companies he has started bloging in 2016.

Recent Posts of blog

Downalod Free Php Scripts