我正在尝试将Cookie设置为仅显示一次弹出窗口,到目前为止,这是我的代码:

jQuery(window).load(function(){
    // Load pop up within parent-page section only
    if (window.location.href.indexOf('parent-page') > -1) {

        alert("your url contains the parent-page in the URL");

        $.magnificPopup.open({
            items: [
                {
                    src: '#disclaimer', // CSS selector of an element on page that should be used as a popup
                    type: 'inline'
                }
            ],
            removalDelay: 300,
            mainClass: 'mfp-fade',
            closeOnContentClick: false,
            modal: true
        });
    }
});

当前,每次在URL中有父页面时都会加载此文件,我只需要显示一次即可。我怎样才能做到这一点?

最佳答案

您可以使用localStorage:

jQuery(window).load(function () {
    if (window.location.href.indexOf('parent-page') > -1 && !localStorage.getItem('popup_show')) {

        $.magnificPopup.open({
            items: [{
                src: '#disclaimer', // CSS selector of an element on page that should be used as a popup
                type: 'inline'
            }],
            removalDelay: 300,
            mainClass: 'mfp-fade',
            closeOnContentClick: false,
            modal: true
        });

        localStorage.setItem('popup_show', 'true'); // Set the flag in localStorage
    }
});



文件:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

关于javascript - 将Cookie设置为仅显示一次弹出窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30887815/

10-09 15:24