我正在尝试在URL中存储四个不同参数的cookie。我让它在检测 cookie 是否存在的地方工作,如果不存在,它会添加它。我还设置了检测它们是否来自具有这些参数的另一个 URL 字符串,但它们是不同的——它会用新的 cookie 信息覆盖旧的 cookie,如果参数为空,它将返回null
。
我在实现时遇到的问题是,如果 URL 字符串包含一组不同的参数,其中至少有一个参数不为 null,我只希望它替换 cookie。如果所有四个参数都为空,但存储了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
}
只需在做饼干之前检查是否有参数......
顺便说一下,您可以使用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();