To pass a parameter in URL in JavaScript, create a
URL
object, then add the parameter with url.searchParams.append()
, finally convert back to string with url.toString()
.Here's how you do it:
const url = new URL('https://whaa.dev');
url.searchParams.append('param', 'value');
const urlString = url.toString();
// https://whaa.dev?param=value
console.log(urlString);
// go to: https://whaa.dev?param=value
window.location.href = urlString;