Skip to content

JavaScript get all URL parameters | Example code

  • by

Use URLSearchParam API to get all URL parameters in JavaScript, it’s supported by all modern browsers.

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

Code for iterate over all the parameters, using for..of:

const params = new URLSearchParams(window.location.search)
for (const param of params) {
  console.log(param)
}

JavaScript get all URL parameters example

HTML example code:-

Let’s do one working example with URL. API which is useful for fiddling around with URL query parameters. Given a URL string, you can easily extract parameter values:

<!DOCTYPE HTML> 
<html> 
<body> 

	<script>
		const site = new URL("https://test.com/hello?name=roger&age=20");

		const params = new URLSearchParams(site.search);
		for (const param of params) {
			console.log(param)
		}

	</script>
</body> 
</html>			

Output:

JavaScript get all URL parameters

Link: Get parameter from URL JavaScript

Do comment if you have any doubts and suggestions on this JS URL topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading