Skip to content

Get parameter from URL JavaScript | Example code

  • by

JavaScript itself has nothing built-in for handling query string parameters. Code running in the latest browser can use the URL object (which is part of the APIs provided by browsers to JS).

Get parameter from URL JavaScript Example code

HTML example code

<!DOCTYPE HTML> 
<html> 
<body> 

	<script>
		var url_string = "http://www.example.com/t.html?name=A&sal=3000&c=xyz"; 

		var url = new URL(url_string);
		var c = url.searchParams.get("c");
		
		console.log(c); 

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

Output:

Get parameter from URL JavaScript

Get parameter from the actual URL JavaScript

You can get the query string from the URL of the current page with:

// https://testsite.com/users?page=10&pagesize=25&order=asc
const urlParams = new URLSearchParams(window.location.search);
const pageSize = urlParams.get('pageSize');

Output: 25

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

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