JavaScript – How To Get All Parameters From URL

In this post, we show you how to get all parameters from URL in JavaScript.

Here is the code example showing how it works.

example.htmlRun Example
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript – How To Get All Parameters From URL</title>
    <style type="text/css">
        table { border: 0px; }
        tr:first-child td { font-weight: bold; }
        tr td { padding: 0 10px; }
    </style>
</head>
<body>
    <table>
        <tbody>
            <tr>
                <td>Parameter name</td>
                <td>Parameter value</td>
            </tr>
        </tbody>
    </table>

    <script type="text/javascript">

        var queryString, queryStringParts,
            extracted, paramName, paramValue;
        
        // get query string from the current URL
        queryString = window.location.search.substr(1);
        queryStringParts = queryString.split('&');
        
        // extract parameter name and value 
        for (var i = 0; i < queryStringParts.length; i++) {
            extracted = queryStringParts[i].split('=');
            paramName = extracted[0];
            paramValue = extracted[1];
            
            var tr = '<tr><td>' + paramName + '</td><td>' + paramValue + '</td></tr>'
            document.getElementsByTagName('tbody')[0].insertAdjacentHTML('beforeend', tr);
        }

        
    </script>

</body>
</html>

The above code, we first get query string from the current URL using window.location.search, then extract parameter name and value from this query string.

Result:
Get all parameters from current URL using JavaScript

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Mayank Upadhyaya Recent comment authors
newest oldest most voted
Mayank Upadhyaya
Guest
Mayank Upadhyaya

hi