Menu

Toshimaru's Blog

Get Query Parameters in JavaScript

Get current URI query params

You can get query parameters from current URL using URLSearchParams.

const params = new URLSearchParams(window.location.search);

Iterate query params

Let’s say your access URL is http://example.com/query_param?a=1&b=2.

You can iterate the query parameters with URLSearchParams and for…of statement.

const params = new URLSearchParams(window.location.search);
for (const [key, value] of params) { console.log(`key: ${key}, value: ${value}`) }

The output:

key: a, value: 1
key: b, value: 2

Get value from key

You can get the value from the key.

const params = new URLSearchParams(window.location.search);
console.log(params.get("a"))
console.log(params.get("b"))

The output:

1
2

Reference

URLSearchParams - Web APIs | MDN

Load more