Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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_Jquery_Cookies - Fatal编程技术网

Javascript 将cookie设置为仅显示一次弹出窗口

Javascript 将cookie设置为仅显示一次弹出窗口,javascript,jquery,cookies,Javascript,Jquery,Cookies,我正在尝试将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");

我正在尝试将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中时,都会加载此文件,我只需显示一次。如何才能做到这一点?

您可以使用来实现这一点:

if (window.location.href.indexOf('parent-page') > -1 && !$.cookie('popup-shown')) {
    $.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
    });
    $.cookie('popup-shown', true);
}

您可以使用
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
    }
});
localStorage属性允许您访问本地存储对象。本地存储类似于会话存储。唯一的区别是,虽然localStorage中存储的数据没有过期时间,但在浏览会话结束时(即浏览器关闭时),sessionStorage中存储的数据将被清除


文档:

正如您所说,将看到的状态存储在cookie或本地存储中!您是否有该路径上的一些代码?取决于您是否希望它在当前会话或x天内不出现?这很好。有没有办法将cookie设置为在10天内过期?@egr103否。
localStorage
没有过期日期谢谢您的回答。由于这使用了一个插件,我确信它非常有效,如果我能像另一个答案所建议的那样使用本地存储实现这一点,我并不特别喜欢在我的网站上加载另一个插件。是的,@Tushar的答案也会很好地工作。注意,如果您想避免使用其他插件,您可以使用原生JS自己获取/设置cookie。