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

How to get multiple parameters from a URL in JavaScript

In this article, you will learn how to get multiple parameters from a URL in JavaScript.

If the URL is the following URL, our task is to store the value of the name parameter in a variable.

https://www.3schools.in/?name=Apu

// const name = Apu;

So for this, we will use the searchParams readonly property of the URL interface that returns a URLSearchParams object. The example is given below.

<script> 
  let myURL = new URL('https://www.3schools.in/?name=Apu').searchParams;
  let name = myURL.get('name');
  console.log(name)
</script>

Suppose, this time the URL has two parameters name and age. Click on the Try it Yourself » button to open the below code in our online editor and open the console.

<script> 
  let myURL = new URL('https://www.3schools.in/?name=Apu&age=19').searchParams;
  let name = myURL.get('name');
  let age = myURL.get('age');
  console.log(name,age)
</script>

I know you're thinking about how to get the parameters from the current URL.

let myURL = new URL(document.location).searchParams;
let name = myURL.get('name');
let age = myURL.get('age');
Try it Yourself »
save
listen
AI Answer
Write Your Answer
loading
back
View All