JavaScript 获取查询字符串参数的值

标签 javascript url split location

我找不到任何分割器解决方案JS来获取此URL中的一个参数 https://www.test.be/fr_BE/home/forms/test-test-salon.html?configId=f4q8s9z1&ucpBaseurl=https%253A%252F%252Ftest.client.ucp.cloud&model=F56&motorization=Diesel

我想要隔离的部分是模型 ID,model=F56,在本例中我想隔离“F56”

我尝试了这个脚本但没有成功:

var srcID = "11111"; // Insert your src ID here
var typeID = "11111"; // Insert your type ID here
var category = "m1111"; // Insert your category value here
var axel = Math.random() + ""; var a = axel * 10000000000000;

// Grab Data Layer
var dataLayerApi = digital.tracking.getInstance(dataLayer);
var digitalData = dataLayerApi.getPageObject((window.minidtm.actPageId     !== undefined) ?  window.minidtm.actPageId  : dataLayerApi.getCurrentPageIndex());

var path = document.location.pathname;
var pathHirarchy = path.split("&");

if(path.match("\/fr_BE\/home\/forms\/.*\/.*") != null) {
            // Get Model Info
            var u1 =  pathHirarchy[1] //U1 - Produit
            var u2 =  pathHirarchy[5].split(".")[0] //U2 - Sous Produit
}
else {

 var u1 = "notApplicable";
 var u2 = "notApplicable";
}

var DCtag = document.createElement("iframe");
DCtag.src = "https://" + srcID + ".fls.test.net/activityi;src=" + srcID + ";type=" + typeID + ";cat=" + category +
        ";u1=" + u1 +";u2=" + u2 + ";dc_lat=;dc_rdid=;    tag_for_child_directed_treatment=;ord=1;num=" + a;
DCtag.width = "1"; DCtag.heigth = "1"; DCtag.frameborder = "0"; DCtag.style =    "display:none"; DCtag.async = true;
document.body.appendChild(DCtag);

你有什么想法吗?专长于?

非常感谢! 卢多游戏

最佳答案

您可以使用URLSearchParams

const params = new URLSearchParams(window.location.search);
const model = params.get('model');

如果查询字符串中不存在 modelparams.get('model') 将返回 null。

有关 URLSearchParams.get 的更多信息,请参阅 MDN documentation

关于JavaScript 获取查询字符串参数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53618273/

相关文章:

javascript - ng-checked 在 angularjs 中不起作用

php - 在 PHP 中从 URL 获取用户名

Python - 从字符串中删除所有标点符号,并仅打印包含 "i"且等于或长于五个字符的单词

javascript - 如何使用 node.js 函数检查我的虚拟机实例在 google-cloud 中使用了多少个 CPU 和内存

javascript - 使 svg 容器在数组循环中出现一个在另一个之下

python - 如何捕获连接到我的网络的设备的 URL

asp.net-mvc - 可以使用 ASP.NET MVC 创建 "controller-less"url 吗?

python - 使用大小列表拆分字符串的 pythonic 方法是什么?

mysql - 如何在mysql中拆分名称字符串?

javascript - 为什么我的异步输入在 useEffect 中未定义?