Read Querystring using Javascript

Many times we come across scenarios where we want to read QueryString value in javascript. There is no direct way in which we can read the value. On server side we can use something like



But in Javascript there is no direct way to read like server side code. Usually these scenarios of reading Query string values from client side come when we are trying to use AJAX on our pages. So what to do in such cases?

Below is small function which will help you to achieve the same very easily!

function getQueryStringValueByName (name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(;
        if (results == null)
            return "";
            return decodeURIComponent(results[1].replace(/\+/g, " "));


Assume we want to retrieve a parameter in query string with name as “pageNumber” then

var qrystring = getQueryStringValueByName("pageNumber");
console.log(qrystring); // log the querystring value for demonstration

Please note: Function will return empty string (“”) if it doesn’t find matching query string.

Happy coding 🙂

Add a Comment

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