GET query string parameters – Cheat

GET query string parameters – Cheat

A utility function to get the value of any query string parameter

We all know that the query string parameters are available for us through window.location (JavaScript Window Location Object). But query string usually turns up as a string and has to be parsed to get the necessary attribute out. There is an easy way through a utility function given below, this can be included in any web deployment:

function getParameterByName(name, url) {
if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\"\\ "\\$&"amp;"amp;");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Usage for query string: ?sith=vader
var foo = getParameterByName('sith'); // "vader"

Enjoy!

Zubair Ahmed's Picture

About Zubair Ahmed

Zubair Ahmed is ambitious tweaker who seeks adventures. He likes slapping the keyboard until something good happens.

Dubai, United Arab Emirates http://grizzlybit.info/about/

Comments