Menu

Showing posts with label How to get query parameter values in JavaScript?. Show all posts
Showing posts with label How to get query parameter values in JavaScript?. Show all posts

How to get query string values in JavaScript?

Using JavaScript now its very easy to get the URL parameters or query parameters or query string using window object. Below is the sample code snippet of reading the URL parameters.

For example we are accessing the below URL where we have two URL parameters present, and we want to read the source and location values from given or any URL.

https://javascript.jorvee.com/geturlparams.html?source=blog&location=javadeveloper

const urlParams = Object.fromEntries(new URLSearchParams(window.location.search).entries());

 

All the URL parameters are now present as an object in urlParams constant. Now to access any url parameter, just access using urlParams.<name of the parameter>. 
e.g. var urlSource = urlParams.source;

Here, source is a url parameter and it will return "blog" as source value.


Learn JavaScript Free | URL parameters and query String
JavaScript