我有以下脚本设置cookie并将值拉入表单。我正在努力的是将失效日期设定为2年?
// Parse the URL
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var keyword = getParameterByName('keyword');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');
var _ga = getParameterByName('_ga');
var gclid = getParameterByName('gclid');
var clientId = getParameterByName('clientId');
// Set the cookies
if(Cookies.set('utm_source') == null || Cookies.set('utm_source') ==
"") {
Cookies.set('utm_source', source, { path: ''});
}
if(Cookies.set('keyword') == null || Cookies.set('keyword') == "") {
Cookies.set('keyword', keyword, { path: '' });
}
if(Cookies.set('utm_medium') == null || Cookies.set('utm_medium') ==
"") {
Cookies.set('utm_medium', medium, { path: '' });
}
if(Cookies.set('utm_campaign') == null || Cookies.set('utm_campaign')
== "") {
Cookies.set('utm_campaign', campaign, { path: '' });
}
if(Cookies.set('_ga') == null || Cookies.set('_ga') == "") {
Cookies.set('_ga', _ga, { path: '' });
}
if(Cookies.set('gclid') == null || Cookies.set('gclid') == "") {
Cookies.set('gclid', gclid, { path: '' });
}
if(Cookies.set('clientId') == null || Cookies.set('clientId') == "") {
Cookies.set('clientId', clientId, { path: '' });
}
// Grab the cookie value and set the form field values
$(document).ready(function(){
$("input[name=utm_source]:hidden").val(source);
$("input[name=keyword]:hidden").val(keyword);
$("input[name=utm_medium]:hidden").val(medium);
$("input[name=gclid]:hidden").val(gclid);
$("input[name=utm_campaign]:hidden").val(campaign);
$("input[name=_ga]:hidden").val(_ga);
$("input[name=clientId]:hidden").val (clientId);
});
</script>
谢谢 - 上面的工作按预期进行,但我很难将Cookie设置为两年后到期 - 我尝试了各种[过期]但无济于事。
答案 0 :(得分:0)
如果有人需要知道,这就是答案......我只是展示了答案的第一个utm_source,但他们都会效仿
if(Cookies.set('utm_source') == null || Cookies.set('utm_source') == "")
{
Cookies.set('utm_source', source, { path: ''});
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 24);
document.cookie = 'utm_source' +"=" + source + ";expires=" + myDate
+ "path=/";
}