Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript mouseleave弹出窗口30天的Cookie功能无法正常工作_Javascript_Cookies - Fatal编程技术网

Javascript mouseleave弹出窗口30天的Cookie功能无法正常工作

Javascript mouseleave弹出窗口30天的Cookie功能无法正常工作,javascript,cookies,Javascript,Cookies,目前,mouseleave弹出窗口正在工作,但希望将cookie与之集成30天 以下是公共开发站点示例: 这是我拼凑的代码,但cookie不起作用: var cookie_length = 30; function setCookie(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exd

目前,mouseleave弹出窗口正在工作,但希望将cookie与之集成30天

以下是公共开发站点示例:

这是我拼凑的代码,但cookie不起作用:

var cookie_length = 30;

function setCookie(c_name,value,exdays) {
   var exdate=new Date();
   exdate.setDate(exdate.getDate() + exdays);
   var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
   document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
   var i,x,y,ARRcookies=document.cookie.split(";");
   for (i=0;i<ARRcookies.length;i++) {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^s+|s+$/g,"");
      if (x==c_name) {
         return unescape(y);
      }
   }
}

var show = getCookie("diyrvcooke");
if(show==null || show=="" || 1) {
    setCookie('diyrvcooke','1',cookie_length);
    //show popup here
}
试着改变

ifshow==null | | show==1{

ifshow==null | | show=={

在您的代码中,您正在检查1,然后将cookie设置为1,这样它就不会被设置,或者如果它以某种方式被设置,将保持设置状态

JS


你收到了什么错误信息?Jonathan,我没有收到错误,只是刷新后弹出窗口一直出现,所以没有cookie,我在orig帖子中包含了演示链接。Thanks@user3923622您没有设置cookie,请参见下面的答案…show变量未定义null@abc123谢谢你,我更新了密码,但是运气不好每次刷新后,pup都会在mouseleave之后出现,但仍然不需要最后一部分…只要ifshow==null | | show=={因为它将在30天后过期,所以显示为未定义的非空。。谢谢,我更改了cookie,但是cookie仍然不存在,请查看我添加的原始帖子中的演示站点。@user3923622该页面上没有与cookieselzi相关的javascript,从查看的第78行开始source@user3923622看起来像是缓存问题:
var cookie_length = 30;

function setCookie(c_name,value,exdays) {
   var exdate=new Date();
   exdate.setDate(exdate.getDate() + exdays);
   var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString());
   document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
   var i,x,y,ARRcookies=document.cookie.split(";");
   for (i=0;i<ARRcookies.length;i++) {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^s+|s+$/g,"");
      if (x==c_name) {
         return unescape(y);
      }
   }
}

var show = getCookie("diyrvcooke");
//show will be undefined the first time before a cookie is set.
if(show===undefined) {
    setCookie('diyrvcooke','1',cookie_length);
    //show popup here
    alert('popup here');
}