javascript cookie上的6个月到期日期

时间:2017-08-25 16:59:28

标签: javascript cookies

我使用javascript并不是很好,但我有一个正在运行,但似乎它没有遵循我设置的6个月到期日期。有人可以帮忙排除故障吗?这是我在设置cookie的到期日期时所拥有的:

expDate = new Date;
// in the following line, 180 means 180 days. 
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000); 
expDate.toGMTString();  

function setCookie(name, value, expires, path, domain, secure){

document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

function getCookie(name){........

任何帮助排除故障都是完美的,非常感谢!

更新 - 这是完整的脚本。我只是打电话给弹出窗口,以显示它是否已进行3次页面访问,以及20%和6个月的到期日期。只需将它拼凑在一起就可以轻松上手!!

expDate = new Date;
// in the following line, 180 means 180 days. 
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000); 
expDate.toGMTString();  

function setCookie(name, value, expires, path, domain, secure){

document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

function getCookie(name){

var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1){
begin = dc.indexOf(prefix);
if (begin != 0) return null;}
else{begin += 2;}
var end = document.cookie.indexOf(";", begin);
if (end == -1){end = dc.length;}
return unescape(dc.substring(begin + prefix.length, end));
}


    visits = getCookie('nVisits');
    if (!visits) {
        visits = 1
    };
    if (visits == 3) 
    if ((Math.random() * (100 - 1) + 1) < 20)
    {
      window.open("https://mypoup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
    }
    if (visits < 3) {
        ++visits;
        cookieData = visits;
        setCookie('nVisits', cookieData, expDate)
    }

1 个答案:

答案 0 :(得分:0)

Kaddath的评论解决了这个问题:

  

似乎toGMTString()已过时且不应再使用,请改用toUTCString()