我试图在location.queryString对象中获取值我得到了两个值,我想将它们存储在一个cookie中,然后我可以在结账过程中将其传递给api。
function getParameterByName( name ){
var regexS = "[\\?&]"+name+"=([^&#]*)",
regex = new RegExp( regexS ),
results = regex.exec( window.location.search );
if( results === null ){
return "";
} else{
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
}
var mc_emailId = getParameterByName('mc_eid');
var mc_campaignId = getParameterByName('mc_cid');
var mccookie = new Array([]);
document.cookie = mccookie;
alert(document.cookie);
我停了一秒钟,因为我不认为我正确地这样做是因为我想要完成的事情。在location.queryString中,我需要将两个值传递给cookie。 mc_eid和mc_cid所有代码都运行正常,但我不确定如何定位这些代码。
根据以下建议更新
// Mail Chimp ID's Stored in Variable
function getParameterByName( name ){
var regexS = "[\\?&]"+name+"=([^&#]*)",
regex = new RegExp( regexS ),
results = regex.exec( window.location.search );
if( results === null ){
return "";
} else{
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
}
var mc_emailId = getParameterByName('mc_eid');
var mc_campaignId = getParameterByName('mc_cid');
mccookie = "mc_eid=" + mc_emailId + "; mc_cid=" + mc_campaignId;
document.cookie = mccookie;
var days = 30;
var d = new Date();
d.setTime(d.getTime() + (days*24*60*60*1000));
var expires =";expires="+d.toUTCString();
document.cookie = mccookie + expires + "; path=/";
答案 0 :(得分:0)
Cookie字符串具有每个Cookie的名称和值,例如name=value; name2=value;
对于您的代码,您可以执行类似
的操作mccookie = "mc_eid=" + mc_emaild + "; mc_cid=" + mc_campaignId;
document.cookie = mccookie;
警告document.cookie会提醒整个cookie字符串。 W3Schools has some functions you could use to get a cookie by name or set one.
设置过期日期
var days = 30;
var d = new Date();
d.setTime(d.getTime() + (days*24*60*60*1000));
var expires =";expires="+d.toUTCString();
您还希望使用path
在整个网站上访问Cookie。在您按上述方式设置expires
和mccookie
之后,您应该最终设置这样的Cookie
document.cookie = mccookie + expires + "; path=/";