Query string are helpful when need to pass values to a page. It start with ? and multiple statement can be added using &
We can also use window.location.href to grab the current page URL in JavaScript
Here is an example query string
example.com/?pid=1001&type=electronics
Using JavaScript we can access the query string easily with window.location.search. Let’s have have query search example
const queryString = window.location.search; console.log(queryString); // ?pid=1001&type=electroni
Find a Parameter
To find/sear a URL Parameter say PID , we need to use URLSearchParams and using the output we can access the param value too as follows
const urlParams = new URLSearchParams(queryString); const pid = urlParams.get('pid') console.log(pid);
Following JavaScript post may help you
- How to resolve sequelize-JSON.parse error in Node - How to resolve JSON.Parse error in sequelize
- Deno : next generation Node - About Deno TypeScript Runtime
- How to extract query string from url parameter using JavaScript - How to access the query string/url parameter of a url in JavaScript
- How to access URL of current page in JS - How to access current page url in JavaScript
- How to pass python list to JavaScript in Flask app - How to pass a python list to JavaScript with ninja template in Flask web app