gpt4 book ai didi

javascript - 如何更新 for 循环以输出参数 URL 键值对?

转载 作者:行者123 更新时间:2023-12-02 22:59:03 25 4
gpt4 key购买 nike

我有一个代码块,它输出在参数 URL 的查询字符串中找到的每个键值对。

我的问题是,我希望更新代码块,以便在analytics.track() 调用中将标准 utm 参数作为属性发送。附加要求是:

  • 仅将官方支持的 utm 参数作为属性发送
  • 仅发送 URL 查询字符串中包含的 utm 参数(即不发送 null utm 参数)
  • 将跟踪调用命名为:“用户推荐”
    var output = {};
var qs = document.location.search.substring(1);
qs = qs.split('&');

for (var i = 0; i < qs.length; i++) {
var tokens = qs[i].split('=');
output[tokens[0].toLowerCase()] = tokens[1];
}

return output;
}

最佳答案

2019 年末就没有必要进行所有这些字符串解析。有 built-ins做这些事情。

let output = (new URL(document.location)).searchParams;
for (const [key, value] of output) {
console.log(key, '=', value);
}

关于javascript - 如何更新 for 循环以输出参数 URL 键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com