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

0 0 vote
Article Rating
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mayank Upadhyaya
Mayank Upadhyaya
1 year ago

hi

1
0
Would love your thoughts, please comment.x
()
x