【问题标题】:Get constant Variable from URL String从 URL 字符串获取常量变量
【发布时间】:2019-01-31 08:36:27
【问题描述】:

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

【问题讨论】:

标签: javascript url getvalue


【解决方案1】:

您可以使用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"));

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 2012-03-17
    • 2022-10-21
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2011-12-05
    相关资源
    最近更新 更多