Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果用户按下关闭按钮,则设置cookie_Javascript_Cookies - Fatal编程技术网

Javascript 如果用户按下关闭按钮,则设置cookie

Javascript 如果用户按下关闭按钮,则设置cookie,javascript,cookies,Javascript,Cookies,如果苏联已经通过closeBtn关闭了弹出窗口,在明天之前不要再显示。 如果它是有可能使其显示后,2访问页面弹出窗口。启动的设置超时正常 jQuery(function ($) { var check_cookie = $.cookie('newsletter_popup'); if (check_cookie == null || check_cookie == 'shown') { setTimeout("beginNewsletterForm()", 120

如果苏联已经通过closeBtn关闭了弹出窗口,在明天之前不要再显示。 如果它是有可能使其显示后,2访问页面弹出窗口。启动的设置超时正常

jQuery(function ($) {
    var check_cookie = $.cookie('newsletter_popup');
    if (check_cookie == null || check_cookie == 'shown') {
        setTimeout("beginNewsletterForm()", 120000);
    }

    $('#newsletter_popup_dont_show_again').on('change', function () {
        if ($(this).length) {
            var check_cookie = $.cookie('newsletter_popup');
            if (check_cookie == null || check_cookie == 'shown') {
                $.cookie('newsletter_popup', 'dontshowitagain');
            }
            else {
                $.cookie('newsletter_popup', 'shown');
                setTimeout("beginNewsletterForm()", 120000);
            }
        } else {
            $.cookie('newsletter_popup', 'shown');
        }
    });
});

function beginNewsletterForm() {
    jQuery.fancybox({
        'padding': '0px',
        'autoScale': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'type': 'inline',
        'href': '#newsletter_popup',
        'onComplete': function () {
            $.cookie('newsletter_popup', 'shown');
        },
        'tpl': {
            closeBtn: '<a title="Close" class="fancybox-item fancybox-close fancybox-newsletter-close" href="javascript:;"></a>'
        }
    });
    jQuery('#newsletter_popup').trigger('click');
}
jQuery(函数($){
var check_cookie=$.cookie(“时事通讯弹出窗口”);
if(check_cookie==null | | check_cookie=='show'){
setTimeout(“beginNewsletterForm()”,120000);
}
$(“#时事通讯(弹出)(不再显示)”)。在('change',function(){
如果($(此).length){
var check_cookie=$.cookie(“时事通讯弹出窗口”);
if(check_cookie==null | | check_cookie=='show'){
$.cookie('时事通讯弹出窗口','dontshowitagain');
}
否则{
$.cookie(“时事通讯弹出窗口”,“显示”);
setTimeout(“beginNewsletterForm()”,120000);
}
}否则{
$.cookie(“时事通讯弹出窗口”,“显示”);
}
});
});
函数beginNewsletterForm(){
jQuery.fancybox({
“填充”:“0px”,
“自动缩放”:正确,
“transitionIn”:“fade”,
“transitionOut”:“fade”,
“类型”:“内联”,
'href':'#时事通讯_弹出窗口',
“onComplete”:函数(){
$.cookie(“时事通讯弹出窗口”,“显示”);
},
“第三方物流”:{
closeBtn:'
}
});
jQuery(“#时事通讯_弹出窗口”).trigger('click');
}

您可以在JAVASCRIPT中创建一个函数,用于检查是否已显示弹出消息以及getCookie函数和setCookie

//execute this function when your document loads
popupChecker();

function popupChecker()
{
   if(getCookie("popup") != "true")
   {
        //show pop up
   }
}

//chage closeBtnId with your close btn ID

$("closeBtnId").click()
{
    //set cookie that expires in 1 day
    setCookie("popup", "true", 1);
}
这是获取和设置Cookie的函数。请将其粘贴到.JS文件中,或者为干净的代码创建新的Cookie类

function setCookie(cname, cvalue, exdays) 
{
   var d = new Date();
   d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
   var expires = "expires="+d.toUTCString();
   document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}


function getCookie(cname) 
{
   var name = cname + "=";
   var decodedCookie = decodeURIComponent(document.cookie);
   var ca = decodedCookie.split(';');
   for(var i = 0; i <ca.length; i++) 
   {
     var c = ca[i];
     while (c.charAt(0) == ' ') 
     {
        c = c.substring(1);
     }
     if (c.indexOf(name) == 0) 
     {
        return c.substring(name.length, c.length);
     }
   }
return "";
}
要了解更多关于使用JS的Cookie的信息,您可以访问此网站
非常感谢。如果用户停留在网站上并浏览2-3页,则在“如果可以在访问2页后显示弹出窗口”选项中显示弹出窗口。欢迎使用。那么你想在用户选择其他页面时弹出吗?您可以做的是,每次用户单击其他页面上的链接或按钮时,添加获取访问计数cookie并添加1,然后再次调用弹出检查程序。只要修改代码,它将帮助你很多!
function popupChecker()
{
   if(getCookie("popup") == "true")
   {
      //check if visit count is equals to 2 
     if(getCookie("visit_count") == "2")
     {
        //show pop up
     }

   }else
   {
       //show pop up and add 1 to visit_count cookie
       var visitCount = getCookie("visit_count");
       setCookie("visit_count", visitCount + 1, 1);
   }
 }

//chage closeBtnId with your close btn ID

$("closeBtnId").click()
{
   setCookie("popup", "true", 1);
   setCookie("visit_count", "1", 1);
}