JavaScript change URL parameter | Example code

One way is to fetch the current URL and convert it to a string. Once you got the string then use replace method to change the URL parameter in JavaScript.

Example of change URL parameter in JavaScript

HTML Example code:

Use String.replace for replace the value of parameter “par=1” to some new value. Changing value if some cases it may not be available.

<!DOCTYPE HTML>
<html>

<body>

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

		var newLoc = url.toString().replace('par=1', 'foo=false');

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

Output:

JavaScript change replace URL parameter

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.