admin管理员组

文章数量:1335682

I'm trying to setup a cookie to only show a popup once, here's my code so far:

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
        });
    }
});

Currently this loads every time parent-page is in the URL, I need to only show it once. How can I do this?

I'm trying to setup a cookie to only show a popup once, here's my code so far:

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
        });
    }
});

Currently this loads every time parent-page is in the URL, I need to only show it once. How can I do this?

Share Improve this question edited Jun 17, 2015 at 9:39 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jun 17, 2015 at 9:38 egr103egr103 4,01815 gold badges71 silver badges120 bronze badges 2
  • As you said either by storing the seen state in a cookie or local storage! Do you have some code along that path? – lshettyl Commented Jun 17, 2015 at 9:43
  • Depends do you want that it not appear for the current session or for x days? – Dekron Commented Jun 17, 2015 at 9:45
Add a ment  | 

2 Answers 2

Reset to default 4

You can use the jQuery cookie plugin to achieve this:

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);
}

You can use 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
    }
});

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the browsing session ends - that is when the browser is closed.

Docs: https://developer.mozilla/en-US/docs/Web/API/Window/localStorage

本文标签: javascriptSetting a cookie to only show popup onceStack Overflow