Skip to content

Javascript add a parameter to the URL without reloading | Example code

  • by

Use the pushState or replaceState methods to Appending/Modifying parameter to URL without refresh/reloading the page in JavaScript.

pushState Method

window.history.pushState("object or string", "Title", "new url");

Or

replaceState Method

window.history.replaceState(null, null, "?arg=123");

JavaScript adds a parameter to URL without reloading Example

HTML Example code

Current page URL update with with argument. This should update the URL without refresh.

<html>

<body>

	<script>

		var currentURL = window.location.protocol + "//" + window.location.host + window.location.pathname + '?arg=1';    

		window.history.pushState({ path: currentURL }, '', currentURL);
	</script>
</body>
</html>	

Output:

JavaScript add a parameter to the URL without reloading

Update static URL

<!DOCTYPE HTML>
<html>

<body>

	<script>
		var url = new URL("https://www.eyehunts.com");

		var newURL = url + '?arg=1';    

		console.log(newURL.toString());
	</script>
</body>
</html>					

Output: https://www.eyehunts.com/?arg=1

Do comment if you have any doubts and suggestion 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 *