Dynamically add a parameter to URL JavaScript | Example code

Using pushState method you can add or modify the URL parameters without using jQuery.

Dynamically add a parameter to URL JavaScript Example

HTML example code:

On click button the web page URL will update.

<!DOCTYPE HTML>
<html>

<body>
	<button onclick="updateURL();">Update</button>

	<script type="text/javascript">
		
		function updateURL() {
			if (history.pushState) {
				var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?para=hello';

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

Output:

Dynamically add a parameter to 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.