Skip to content

JavaScript get query string | Current URL example

  • by

Use URLSearchParam API to get query string in JavaScript. It’s supported by all modern browsers. But you have to first get the full query string via the window.location.search property.

console.log(window.location.search);
 // "?post=1234&action=edit"

var urlParams = new URLSearchParams(window.location.search);

console.log(urlParams.has('post')); // true

How to get query string Example

For current URL use it:-

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

How it will work if we have a URL like this:-

“https://test.com?ProjectID=462”

Complete code

<html>
<body>

	<script>
		const params = new URL("https://test.com?ProjectID=462").searchParams;

		for (const param of params) {
			console.log(param)
		}

	</script>

</body>
</html>

Output:

JavaScript get query string

Do comment if you have any suggestions on this JS URL code.

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

Your email address will not be published. Required fields are marked *