xxxxxxxxxx
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5"; //window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);
Run code snippet
xxxxxxxxxx
const url = new URL(window.location.href);
const parameterValue = url.searchParams.get('pramaName');
console.log(parameterValue);
// another method
let url = "https://example.com?param1=value1¶m2=value2";
let params = new URLSearchParams(url);
console.log(params.get("param1")); // "value1"
console.log(params.get("param2")); // "value2"
xxxxxxxxxx
<!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>