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

时间:2017-05-11 17:36:26

标签: javascript cookies null overwrite setcookie

我正在尝试在网址中存储四个不同参数的Cookie。我有它工作的地方,它会检测cookie是否存在,如果不存在,它会添加它。我还设置了如果它们来自具有这些参数的另一个URL字符串但它们是不同的 - 它会用新的cookie信息覆盖旧的cookie,如果参数为空,它将返回{{1 }}

我在实施方面遇到的问题是,如果URL字符串包含一组不同的参数(其中至少有一个参数不为空),我希望它替换cookie。如果所有四个参数都为空,但存储了cookie,我不希望它替换cookie。

这是我的脚本:

null

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

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); } =&gt; domain.com/?utm_source=b&utm_medium=a&utm_campaign=c&utm_content=d

然后他们进来了:

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

然后他们进来了:

sbaValue = a;null;c;d =&gt; domain.com/(之前没有任何变化)

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

2 个答案:

答案 0 :(得分:1)

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

在做饼干之前,只需检查是否有参数...

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

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

答案 1 :(得分:0)

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

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