检查cookie是否存在以进行年龄验证

时间:2014-06-24 12:29:23

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

尝试检查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()
        });
    }
});

1 个答案:

答案 0 :(得分:0)

修复了我的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()
    });
}

});