// https://testsite.com/users?page=10&pagesize=25&order=asc
const urlParams = new URLSearchParams(window.location.search);
const pageSize = urlParams.get('pageSize');
const url = new URL(window.location.href);
const parameterValue = url.searchParams.get('pramaName');
console.log(parameterValue);
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code')
const params = new URLSearchParams(window.location.search);
const something = decodeURIComponent(params.get('hi'));
// or shorter, no need to use variables
decodeURIComponent((new URLSearchParams(window.location.search)).get('hi'))
// Example URL: https://example.com/over/there?name=ferret
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const parameter_name = urlParams.get('name')
console.log(parameter_name);
let url = 'https://www.example.com?name=n1&name=n2';
let params = (new URL(url)).searchParams;
params.get('name') // "n1"
params.getAll('name') // ["n1", "n2"]
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const page_type = urlParams.get('page_type')
console.log(page_type);
<!DOCTYPE html>
<html>
<head>
<title>How To Get URL Parameters using JavaScript?</title>
</head>
<body>
<h1 style="color: blue;">
Softhunt.net
</h1>
<b>
How To Get URL Parameters
With JavaScript?
</b>
<p> The url used is:
https://www.example.com/login.php?a=Softhunt.net&b=Welcome&c=To Our Website
</p>
<p> Click on the button to get the url parameters in the console. </p>
<button onclick="getParameters()"> Get URL parameters </button>
<script>
function getParameters() {
let urlString =
"https://www.example.com/login.php?a=Softhunt.net&b=Welcome&c=To Our Website";
let paramString = urlString.split('?')[1];
let queryString = new URLSearchParams(paramString);
for(let pair of queryString.entries()) {
console.log("Key is:" + pair[0]);
console.log("Value is:" + pair[1]);
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> How To Get URL Parameters using JavaScript? </title>
</head>
<body>
<h1 style="color:blue;">
Softhunt.net
</h1> <b>
How To Get URL Parameters
With JavaScript?
</b>
<b>By Separating and accessing each parameter pair</b>
<p> The url used is:
https://www.example.com/login.php?a=Softhunt.net&b=Welcome&c=To Our Website
</p>
<p> Click on the button to get the url parameters in the console. </p>
<button onclick="getParameters()"> Get URL parameters </button>
<script>
function getParameters() {
let urlString =
"https://www.example.com/login.php?a=Softhunt.net&b=Welcome&c=To Our Website";
let paramString = urlString.split('?')[1];
let params_arr = paramString.split('&');
for(let i = 0; i < params_arr.length; i++) {
let pair = params_arr[i].split('=');
console.log("Key is:" + pair[0]);
console.log("Value is:" + pair[1]);
}
}
</script>
</body>
</html>