How to Get URL Parameter Value in Javascript
In this tutorial, you will learn how to get URL parameter value in javascript. URL parameters are also known as query parameters. These query parameters help the server to fetch and deliver relevant data to the user.
The URL parameters are added after the “?”
symbol and you can add as many URL parameters you want in the URL but you must separate each query parameter with the “&”
symbol.
There are numerous ways to get the values of URL parameters. Most of them involve the usage of regex, but we are going to use URL()
and URLSearchParams()
constructors to accomplish our goal.
In the following example, we have one URL with some query parameters. In the input field, we will enter the name of the query parameter. Upon click of a button, we will get its value from the URL and display it on the screen. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 4 elements in the HTML file (
div
,button
,input
, andh1
). Thediv
element is just a wrapper for the rest of the elements. - We are using
style
attribute withdiv
element to center align all the elements horizontally. - The
innerText
for thebutton
element is“Get”
and for theh1
element, it is“Display Here”
. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div style="text-align: center"> <input type="text" placeholder="Enter Param"> <button>GET</button> <h1>Display Here</h1> </div> <script src="script.js"></script> </body> </html>
Javascript
- We have one URL with query parameters and it is assigned to the
urlString
variable. - We have selected the
button
element,input
element, andh1
element using thedocument.querySelector()
method and stored them inbtnGet
,input
, anddisplay
variables respectively. - We have attached the
click
event listener to thebutton
element. - In the event handler function, we are using a
URL()
constructor and passing our URL string as a parameter. This will return aURL
object and we are storing that in theurl
variable. - We need to extract the query string from the URL. For that reason, we are using the
search
property of theURL
object. This returns the query string along with the“?”
symbol. To get rid of the“?”
symbol, we are using theslice()
method and passing 1 as a parameter. The final query string is stored in thesearchString
variable. - We are using the
URLSearchParams()
interface and passingsearchString
as a parameter. It returns an object and we are storing that in thesearchParams
variable. - We are getting value from the
input
element usingvalue
property and storing it in theinputParam
variable. - The object returned by the
URLSearchParams()
interface containshas()
method. This method takes a string as a parameter and returns a Boolean value. It checks whether the query parameter with a specified name exists or not. - If
True
, then we will get the value of the query parameter by using theget()
method and display it in theh1
element using theinnerText
property. - If
False
, then we will display“Wrong Param”
in theh1
element.
let urlString = 'http://www.example.com/xyz.php?user=peter&age=27&country=usa'; let btnGet = document.querySelector('button'); let input = document.querySelector('input'); let display = document.querySelector('h1'); btnGet.addEventListener('click', () =>{ let url = new URL(urlString); let searchString = url.search.slice(1); let searchParams = new URLSearchParams(searchString); let inputParam = input.value; if(searchParams.has(inputParam)){ display.innerText = searchParams.get(inputParam); }else { display.innerText = 'Wrong Param'; } });