1

我正在使用此代码打开一个 html 弹出窗口,希望向其中添加一个“不再显示”按钮,该按钮将在 cookie 中存储首选项,以便不会再次显示弹出窗口 为什么不它工作吗?

这是应该写入 cookie 的行:

document.cookie = "dontShow=1; path=/; expires=Wednesday, 01-Jan-2020 12:00:00 GMT; domain=.qpcftw.co.cc";

更新:问题是 cookie 没有被存储,并且“alert(document.cookie);” (见下文)表明 cookie 为空(未显示任何内容)。

这是完整的JS代码:

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/
var popupStatus = 0;

$(document).ready(function(){
        if (document.cookie != "dontShow=1; path=/; expires=Wednesday, 01-Jan-2040 12:00:00 GMT; domain=.qpcftw.co.cc"){
        $("#backgroundPopup").css({
            "opacity": "0.75"
        });
        $("#backgroundPopup").fadeIn(1000);
        $("#popupContact").fadeIn(1000);
        popupStatus = 1;
        }
    //CLOSING POPUP
        //Don't show again
        $("#dontShow").click(function(){
            document.cookie = "dontShow=1; path=/; expires=Wednesday, 01-Jan-2020 12:00:00 GMT; domain=.qpcftw.co.cc";
            alert(document.cookie); //4debugging
            disablePopup();
        });
        //Click the x event!
        $("#popupContactClose").click(function(){
            disablePopup();
        });
        //Click out event!
        $("#backgroundPopup").click(function(){
            disablePopup();
        });
        //Press Escape event!
        $(document).keypress(function(e){
            if(e.keyCode==27 && popupStatus==1){
                disablePopup();
            }
        });
});

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut(500);
        $("#popupContact").fadeOut(500);
        popupStatus = 0;
    }
}
4

1 回答 1

1

试试这个:

小提琴演示

document.cookie = 'dontShow=1; expires=Wed, 01 Jan 2020 12:00:00 GMT; path=/';

function readCookie(option) {
    var optionEQ = option + '=';
    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(optionEQ) === 0) {
            return c.substring(optionEQ.length, c.length);
        }
    }

    return false;
}

if (!readCookie('dontShow')) {
    //run your popup code
}

编辑:或者,作为自定义 jQuery 插件:

插件版本小提琴

document.cookie = 'dontShow=1; expires=Wed, 01 Jan 2020 12:00:00 GMT; path=/';

(function($) {
    $.fn.readCookie = function(option) {
        var optionEQ = option + '=';
        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(optionEQ) === 0) {
                return c.substring(optionEQ.length, c.length);
            }
        }

        return false;
    }
})(jQuery);

if (!$(document).readCookie('dontShow')) {
    $('#cookie-status').html('<p>Cookie not set.</p>');
} else {
    $('#cookie-status').html('<p>Cookie has been set.</p>');
}

希望这可以帮助!:)

于 2012-08-09T01:04:58.433 回答