javascript cookie的到期日期不起作用-已更新

时间:2018-06-20 16:33:28

标签: javascript

更新-根据以下建议做出: 我有以下脚本,该脚本在3次访问后弹出一个窗口,只有20%的访问者。到期日期已设置,并显示在开发人员工具中,可以在180天内到期,但仍会弹出。我究竟做错了什么?感谢任何帮助,我一点也不了解javascript。这必须全部用普通的javascript完成-无需jQuery

ClientRequestSwitchboard

我更改为您建议的内容(URL除外-该部分已组成(真正的部分会弹出)。我做了所有建议的更改,但当我更改访问次数时> 3,现在没有任何内容弹出(清除缓存并点击大约50次。

这就是我现在拥有的:

//setting the expiration date to 6 months from now//
expDate = new Date;
// in the following line, 180 means 180 days.// 
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
//making the cookie//
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=/wmb" + path : "") +
    ((domain) ? "; domain=www.example.com" + 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 += 1;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}
//cookie name is called CustomSurvey//
visits = getCookie('CustomSurvey');
if (!visits) {
  visits = 1
};
if (visits < 4)
  //Math.random is set so that 20% of those visits actually get the popup//
  if ((Math.random() * (100 - 1) + 1) < 20) {
    //get location of where this popup came from; also contains variable from SM that will trickle down into reporting to tell where it came from//
    var currentLocation = window.location;
    window.open("https:mysurvey.com" + "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
  }
if (visits < 3) {
  ++visits;
  cookieData = visits;
  setCookie('CustomSurvey', cookieData, expDate)
}

0 个答案:

没有答案
相关问题