【发布时间】:2019-01-31 08:36:27
【问题描述】:
我正在尝试隔离网站 URL 字符串中的常量变量。例如 www.website.com/products/brand-product-description?variant=123456789 。我需要一个只返回'123456789'的js函数
【问题讨论】:
标签: javascript url getvalue
我正在尝试隔离网站 URL 字符串中的常量变量。例如 www.website.com/products/brand-product-description?variant=123456789 。我需要一个只返回'123456789'的js函数
【问题讨论】:
标签: javascript url getvalue
您可以使用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"));
【讨论】: