Hackerss.com

hackerss
hackerss

Posted on

Javascript Obtain current url with query strings


//get the current url
const url = window.location.href;

//get the query string
const queryString = window.location.search;

//get the query string parameters
const params = new URLSearchParams(queryString);

//get the value of a specific parameter
const id = params.get('id');

//output
console.log(url); //http://localhost:3000/index.html?id=1
console.log(queryString); //?id=1
console.log(params); //URLSearchParams { 'id' => '1' }
console.log(id); //1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)