[英]JavaScript get value of query string parameter
我找不到任何拆分器解决方案 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=柴油
我要隔离的部分是模型 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');
如果查询字符串中不存在model
params.get('model')
将返回 null。
有关URLSearchParams.get
更多信息,请参阅MDN 文档
您可以使用正则表达式:
const 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'; const result = url.match(/model=(.*?)&/); const model = result[1]; console.log(model);
编辑:
我要把这个答案留在这里,但 bflemi3 的答案更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.