Lets say you have accessed an url on your site https://my-website.com/search?q=product-x&maxprice=300
.
How can you get the variables search
or maxprice
in javascript ?
You can use this function:
function get(varName, alternative = null)
{
var url = new URL(window.location.href);
var params = new URLSearchParams(url.search);
return params.has(varName) ?
params.get(varName) :
alternative;
}
Then you can use it like this:
var searchFor = get('q'); // product-x
var maxPrice = get('maxprice'); // 300