Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu December 15, 2022 › #dynamically #HowTo

How to add a parameter to the URL using JavaScript

In this article, you will see how we can dynamically add a parameter to a URL in JavaScript.

So we have the following URL and we want to add a parameter to that URL.

How to add a parameter to the URL in JavaScript.

To add a parameter to the URL, we are going to use :

URL.searchParams : the searchParams readonly property of the URL interface returns a URLSearchParams object.

Element.append() : the append() method inserts a set of Node objects or string objects after the last child of an element.

 <button onclick="myFunc()">Add Parameter</button> 
 <div id="my-div"> </div>
<script> 
 let url = new URL("https://www.3schools.in"); 
 const myDiv = document.getElementById('my-div'); 

function myFunc() { 
   url.searchParams.append('parameter', 'value'); 
   myDiv.innerHTML = url; 
} 

</script>  
save
listen
AI Answer
Write Your Answer
loading
back
View All