-1

I am trying to isolate a constant variable in the URL string of a website. For example www.website.com/products/brand-product-description?variant=123456789 . I need a js function that will return only '123456789'

  • 5
    Possible duplicate of [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – jordiburgos Aug 24 '18 at 21:00

1 Answers1

0

You can use the URL object and use its searchParams.get function. In older browsers, you can use this URL polyfill: https://github.com/lifaon74/url-polyfill/blob/master/url-polyfill.js

function getParam(url, param){
  var link = new URL(url);
  return link.searchParams.get(param);
}
console.log(getParam("http://www.website.com/products/brand-product-description?variant=123456789", "variant"));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80