【问题标题】:Display a modal after x pages views with JS Cookie?在使用 JS Cookie 浏览 x 页后显示模式?
【发布时间】:2019-08-23 23:00:54
【问题描述】:

我正在尝试显示在我的网站上查看的 Bootstrap modal avec 3 页面,并使用 JS-Cookie 插件找到this 问题的实际解决方案。

我试过这段代码,但它不起作用:

$(document).ready(function () {
    // create cookie
    var visited = Cookies('visited'); // visited = 0
    if (visited >= 3) {
        // open fancybox after 3 secs on 4th visit or further on the same day
        $('#my_modal').modal('show');
    } else {
        visited++; // increase counter of visits
        // set new cookie value to match visits
        Cookies('visited', visited, {
            expires: 1 // expires after one day
        });
        return false;
    }
}); // ready

上面的代码应该做什么:在 4 页重新加载后提示模式,like here
什么不工作:我重新加载页面但模式没有出现

这是我的 cookie 状态(在 chrome 上): Image on this link

这是一个实际用于在网站首次访问时显示模式的代码示例:

$(document).ready(function() {
  if (Cookies('pop_welcome') == null) {
         $('#my_modal').modal('show');
     Cookies('pop_welcome', '31', { expires: 31 });
  }
 });

** -- 解决方案 -- **

您可以找到解决方案into @greedchikara code snip

代码:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

var visited = readCookie('tester') || 1;

if (visited > 2) {
  $('#my_modal').modal('show');
} else {
  visited++;
  createCookie('tester', visited, 1);
}

你能帮帮我吗? 谢谢

Here is the code of the JS cookie plugin.

【问题讨论】:

  • 检查控制台是否有错误。请注意,Cookies 不是标准方法,因此除非您自己添加,否则会导致错误
  • 感谢您的回答。此代码不会在我的 chrome 控制台上提示任何错误...我使用 Cookies 方法的工作代码片段编辑了我的问题
  • 检查$('#my_modal').modal('show');是否从控制台打开模态
  • 那么Cookies('visited') 会返回什么?您显示它在 cmets 中返回 0。做一些日志记录到控制台,看看发生了什么
  • @greedchikara cookie 没有打开模式。 cookie 存在但没有做任何事情 :'(

标签: javascript jquery cookies js-cookie


【解决方案1】:

您可以使用这三个简单的函数来执行cookie任务,不需要第三方库。

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

【讨论】:

    【解决方案2】:
    $(document).ready(function() {
      // create cookie
      var visited = $.cookie('visited'); // visited = 0
      if (visited >= 3) {
        setTimeout(function() {
          $('#my_modal').modal('show'); //make sure model is already created with id my_model 
        }, 1000);
      } else {
        visited++; // increase counter of visits
        // set new cookie value to match visits
        $.cookie('visited', visited, {
          expires: 1 // expires after one day
        });
        return false;
      }
    });
    
    
    Make sure all dependent js are included.
    

    jQuery 插件在这里 https://github.com/carhartl/jquery-cookie

    【讨论】:

    • 回答没有解释发生了什么变化以及为什么几乎没用
    • 我已经在我的系统上测试过了,请确保你添加了正确的 jquery cookie 插件。
    • 我@NaruKeer,JqueryCookie 插件已被弃用,我使用他的替代者 JSCookie
    • 第一次加载页面时,cookie 返回 NaN 值来处理这个添加 if(typeofvisited=='undefined'){ varvisited=0;} 就在 if 条件之前,就像你正在做的那样visited++ 所以这个变量应该有整数值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 2018-02-21
    • 2012-07-20
    相关资源
    最近更新 更多