Skip to content

JavaScript change URL parameter | Example code

  • by

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 replacing the value of parameter “par=1” with some new value. Changing value if in 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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading