Add Cookie to Fancy Box Popup

試著忘記壹切 提交于 2020-01-16 02:49:15

问题


I have this POPUP I can use to popup constantly - I wanted to add a 7 day cookie to it? Is that possible with what I have?

<script type="text/javascript">
jQuery(document).ready(function() {
    $.fancybox(
        '<h2>' + {{ settings.popup_title | json }} + '</h2><p>' + {{ settings.popup_content | json }} + '</p>',
        {
             'autoDimensions'   : false,
            'width'                 : 450,
            'height'                : 'auto',
            'transitionIn'      : 'none',
            'transitionOut'     : 'none'
        }
    );
});
</script>

回答1:


Based on this answer, you can tweak the code with what you have like :

function openFancybox() {
    setTimeout(function () {
        $.fancybox(
            '<h2>' + {
            {
                settings.popup_title | json
            }
        } + '</h2 > < p > ' + {
            {
                settings.popup_content | json
            }
        } + ' < /p>', {
            'autoDimensions': false,
            'width': 450,
            'height': 'auto',
            'transitionIn': 'none',
            'transitionOut': 'none'
        });
    }, 5000);
}
$(document).ready(function () {
    var visited = $.cookie('visited');
    if (visited == 'yes') {
        return false;
    } else {
        openFancybox();
    }
    $.cookie('visited', 'yes', {
        expires: 7
    });
});

This will trigger fancybox after 5 seconds (or the time you prefer) on first visit only and during the following 7 days, assuming that the visitors haven't cleared their browser's cache/cookies.

Make sure you have properly loaded the jQuery cookie plugin, as well as jQuery + fancybox js and css files.




回答2:


<style>
#holidayPopupContent 
        {   display: none;
        }
        .fancybox-opened #holidayPopupContent { display: block; }
</style>
<div id="holidayPopupContent">
        test
    </div>

    <a href="#holidayPopupContent" id="holidayPopup"></a>

<script type="text/javascript">
function openFancybox() {
    setTimeout( function() 
    {
        jQuery("#holidayPopup").fancybox({
        'width': 880,
        'height': 670,
        'autoSize': false
    }).trigger('click');
        //jQuery('#holidayPopup').trigger('click'); 
        jQuery(".fancybox-skin").css("padding", "0px");
    },200);
}
jQuery(document).ready(function() {
    var visited = jQuery.cookie('visited');
    if (visited == 'yes') {
        return false;

    } else {
        openFancybox();
    }
    jQuery.cookie('visited', 'yes', { expires: 30 });

});
</script>


来源:https://stackoverflow.com/questions/18833775/add-cookie-to-fancy-box-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!