-2

我试图在网站的 URL 字符串中隔离一个常量变量。例如 www.website.com/products/brand-product-description?variant=123456789 。我需要一个只返回'123456789'的js函数

4

1 回答 1

0

您可以使用URL对象并使用它的searchParams.get功能。在旧版浏览器中,您可以使用此 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"));

于 2018-08-24T21:15:35.230 回答