最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url - JavaScript get value of query string parameter - Stack Overflow

programmeradmin2浏览0评论

I can't find any splitter solution JS to obtain one parameter in this URL .html?configId=f4q8s9z1&ucpBaseurl=https%253A%252F%252Ftest.client.ucp.cloud&model=F56&motorization=Diesel

The part I want to isolete is the model id, model=F56, in this case I want to isolate "F56"

I tried this script without success:

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/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);

Do you have an idea ? Expertize in ?

Big big Thanks ! Ludo

I can't find any splitter solution JS to obtain one parameter in this 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

The part I want to isolete is the model id, model=F56, in this case I want to isolate "F56"

I tried this script without success:

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/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);

Do you have an idea ? Expertize in ?

Big big Thanks ! Ludo

Share Improve this question edited Dec 11, 2018 at 14:04 bflemi3 6,79021 gold badges95 silver badges158 bronze badges asked Dec 4, 2018 at 17:15 Ludovic WyboLudovic Wybo 232 silver badges5 bronze badges 1
  • 1 new URLSearchParams(document.location.search).get('model') – Thomas Commented Dec 4, 2018 at 17:19
Add a ment  | 

2 Answers 2

Reset to default 7

You could use URLSearchParams

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

If model isn't present in the query string params.get('model') will return null.

For more information on URLSearchParams.get see the MDN documentation

You could use a regexp:

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);

EDIT:

I'm gonna leave this answer here, but bflemi3's answer is better.

发布评论

评论列表(0)

  1. 暂无评论