Skip to main content

How to get query string value in javascript

You can get the query string value in javascript by passing the query key to function and this function will return the value of this specific query key.

  • Get the query string value using javascript

    <script>
    //URI = http://website.test/search?keyword=samar
    function GetParameterValues(param) {  
        var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
        for (var i = 0; i < url.length; i++) {  
            var urlparam = url[i].split('=');  
            if (urlparam[0] == param) {  
                return urlparam[1];  
            }  
        }  
    }
    
    var searchedKeyword = GetParameterValues('keyword');
    console.log(searchedKeyword);
    //output : samar
    </script>

    This code snippet will help you to get the query string value using javascript. You have to create a function which return the value of query string by specifying the query key to javascript function.

  • Back to code snippet queries related javascript