仅在存在其他参数时覆盖 cookie

Only overwriting cookie when other parameters are present

我试图在 URL 中存储四个不同参数的 cookie。我让它工作,它会检测 cookie 是否存在,如果不存在,它会添加它。我还设置了检测它们是否来自另一个带有这些参数的 URL 字符串,但它们是不同的——它会用新的 cookie 信息覆盖旧的 cookie,如果参数为空,它会return null.

我在实施时遇到的困难是,如果 URL 字符串包含一组不同的参数,我 希望它替换 cookie,其中至少有一个其中之一不为空。如果四个参数都为null,但是存了cookie,我不想用它替换cookie。

这是我的脚本:

function queryParam(name, url) {
   if (!url) url = window.location.href;
   name = name.replace(/[\[\]]/g, "\$&");
   var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
       results = regex.exec(url);
   if (!results) return null;
   if (!results[2]) return '';
   return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var source = queryParam('utm_source'); 
var campaign = queryParam('utm_campaign'); 
var medium = queryParam('utm_medium'); 
var content = queryParam('utm_content'); 


function setCookie(utm, utmvar, exdays) {
   var d = new Date();
   d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
   var expires = "expires="+d.toUTCString();
   document.cookie = utm + "=" + utmvar + ";" + expires + ";path=/";
}

function getCookie(utm) {
   var name = utm + "=";
   var ca = document.cookie.split(';');
   for(var i = 0; i < ca.length; i++) {
       var c = ca[i];
       while (c.charAt(0) == ' ') {
           c = c.substring(1);
       }
       if (c.indexOf(name) == 0) {
           return c.substring(name.length, c.length);
       }
   }
   return "";
 }

function checkCookie() {

var utmSource = getCookie("utm_source");
if (utmSource != "") {  
    if (utmSource = source) {
        utmSource = utmSource; 
    }
    else { 
        setCookie("utm_source", utmSource, 7);
    }
} 
else {
    utmSource = source;
    if (utmSource != "" && utmSource != null) {
        setCookie("utm_source", utmSource, 7);
    }
}

var utmCampaign = getCookie("utm_campaign");
if (utmCampaign != "" ) {
    if (utmCampaign = campaign) {
        utmCampaign = utmCampaign; 
    }
    else {
        setCookie("utm_campaign", utmCampaign, 7);
    }
} 
else {
    utmCampaign = campaign;
    if (utmCampaign != "" && utmCampaign != null) {
        setCookie("utm_campaign", utmCampaign, 7);
    }
}
var utmMedium = getCookie("utm_medium");
if (utmMedium != "" ) { 
    if (utmMedium = medium) {
        utmMedium = utmMedium; 
    }
    else {
        setCookie("utm_medium", utmMedium, 7);
    }
} 
else {
    utmMedium = medium;
    if (utmMedium != "" && utmMedium != null) {
        setCookie("utm_medium", utmMedium, 7);
    }
}
var utmContent = getCookie("utm_content");
if (utmContent != "") { 
    if (utmContent = content) {
        utmContent = utmContent; 
    }
    else {
        setCookie("utm_content", utmContent, 7);
    }
}
else {
    utmContent = content;
    if (utmContent != "" && utmContent != null) {
        setCookie("utm_content", utmContent, 7);
    }
}

var sbaValue = utmCampaign + ";" + utmSource + ";" + utmMedium + ";" + utmContent;

    $("#00N0B000005VTnf").attr("value", sbaValue);

}

所以我想要如何行动的一个例子是 - 如果 URL 字符串是:

domain.com/?utm_source=b&utm_medium=a&utm_campaign=c&utm_content=d => sbaValue = a;b;c;d

然后他们进来:

domain.com/?utm_medium=a&utm_campaign=c&utm_content=d => sbaValue = a;null;c;d

然后他们进来:

domain.com/ => sbaValue = a;null;c;d(与之前没有变化)

我尝试在某些内容不为空时设置一个变量,但无论我如何实现它,我都无法让它工作。

if(["utm_campaign","utm_whatever"].some(el=>queryParam(el)){
 ... change
}

在做cookiestuff之前简单地检查是否有参数...

顺便说一下,使用 OR 运算符可以节省很多时间:

setCookie("utm_source",queryParam("utm_source")||getCookie("utm_source")||alternative, 7);

我最终重写了很多,并清理了很多。这是最终解决方案:

function queryParam(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}


function setCookie(utm, utmvar, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = utm + "=" + utmvar + ";" + expires + ";path=/";
}

function getCookie(utm) {
    var name = utm + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function getQueryStringVariables(){
    return {
        source: queryParam('utm_source'), 
        campaign: queryParam('utm_campaign'),
        medium: queryParam('utm_medium'), 
        content: queryParam('utm_content'), 
    }
}

function isQueryStringSet(){
    var queryStringVariables = getQueryStringVariables();
    if (queryStringVariables.source ||
        queryStringVariables.campaign ||
        queryStringVariables.medium ||
        queryStringVariables.content) {
        return true;
    }
    return false;
}

function fetchCookieData() {
    return {
        source: getCookie("utm_source"),
        campaign: getCookie("utm_campaign"),
        medium: getCookie("utm_medium"),
        content: getCookie("utm_content"),
    };
};

function updateCookie() {
    var queryStringVariables = getQueryStringVariables();
    if (isQueryStringSet()) {
        setCookie("utm_source", queryStringVariables.source, 7);
        setCookie("utm_campaign", queryStringVariables.campaign, 7);
        setCookie("utm_medium", queryStringVariables.medium, 7);
        setCookie("utm_content", queryStringVariables.content, 7);
    }
}

function updateFormValue() {
    var cookieData = fetchCookieData();
    var sbaValue = cookieData.campaign + ";" + cookieData.source + ";" + cookieData.medium + ";" + cookieData.content;

    $("#00N0B000005VTnf").attr("value", sbaValue);
}

updateCookie();
updateFormValue();