【问题标题】:Check if cookie exists for age verification检查 cookie 是否存在用于年龄验证
【发布时间】:2014-06-24 21:40:19
【问题描述】:

尝试检查 cookie 是否存在。如果没有,则启动基础显示模式。单击模态框上的按钮设置一个新的 cookie,以便下次加载页面时不会加载模态框。

我设法设置了 cookie,但我的检查似乎不起作用,因为即使 cookie 在我的浏览器历史记录中,我的模式框也会出现。

$(document).ready(function () {

    if ($.cookie('ageVerification') == undefined || $.cookie('ageVerification') == null || $.cookie('ageVerification') != 'Verification that user is over the legal drinking age in your country') {

        // Reveal modal
        $("#myModal").reveal();

        // Set Cookie on click 
        var ageVerification = document.getElementById('ageVerification');

        ageVerification.addEventListener('click', function (event) {
            $.cookie('ageVerification', 'Verification that user is over the legal drinking age in there country', {
                expires: 10, //expires in 10 days

                path: '/', //The value of the path attribute of the cookie 
                //(default: path of page that created the cookie).

                domain: 'becketts.knibbshost.co.uk', //The value of the domain attribute of the cookie
                //(default: domain of page that created the cookie).

                secure: true //If set to true the secure attribute of the cookie
                //will be set and the cookie transmission will
                //require a secure protocol (defaults to false).
            });
            $('#myModal').hideModal()
        });
    }
});

【问题讨论】:

  • 您的 cookie 实际上并没有被设置在首位 becketts.knibbshost.co.uk
  • 在您的 cookie 检查中取出最后一个“或”语句。如果它有效,那么字符串比较中可能缺少一些东西,可能是结束标点符号 (.)?

标签: javascript jquery cookies jquery-cookie zurb-reveal


【解决方案1】:

修复了我的 cookie 集上的额外设置,无法正常工作。

answer 需要以下脚本才能运行:

$(document).ready(function () {

if ($.cookie('ageVerification') == undefined || $.cookie('ageVerification') == null) {

    // Reveal modal
    $("#myModal").reveal();

    // Set Cookie on click 
    var ageVerificationBtn = document.getElementById('ageVerification');

    ageVerificationBtn.addEventListener('click', function (event) {
        $.cookie('ageVerification', 'Verification that user is over the legal drinking age in there country', {
            expires: 10, //expires in 10 days
        });
        $('#myModal').hideModal()
    });
}

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2011-09-17
    • 2022-01-22
    • 2010-12-21
    • 2016-12-02
    • 2014-05-29
    • 2017-11-27
    相关资源
    最近更新 更多