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.



		var url = new URL("");

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



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

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

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