使用JS Cookie在x页面浏览后显示模式?

时间:2019-04-02 14:57:21

标签: javascript jquery cookies js-cookie

我试图显示在我的网站上查看的Bootstrap模态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.

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:-1)

$(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