Michele B.
Michele B.

Reputation: 11

sessionStorage not persisting when I return to page

I am using a popup when visitors get to a certain section of the site. When a visitor hits the x the popup closes, and I want this state to persist throughout their visit so they don't get annoyed. But if they close their browser and come back to the site another day, I would like the popup to show again.

In Chrome desktop browser: Right now, it is working where the popup shows, if I click the x it closes and if I click deeper into other pages on the site, it doesn't show again, but if I go back to the top-level page it pops up again. And vice versa, if I enter on an interior page and hit close it works, but if I get to the top level page it shows again. What am I doing wrong?

require(["jquery", "domReady!"], function ($) {
// mobile
$(window).on('touchmove', function () {
    if (($(window).scrollTop() > $(window).height() / 2) && 
sessionStorage.getItem('semCroPopupDisabled') !== "true" ) {
        $('#croWrapper').animate({
            bottom: 0
        }, 'fast');
    }
});

// desktop
$(window).on('scroll', function () {
    if (($(window).scrollTop() > $(window).height() / 2) && 
sessionStorage.getItem('semCroPopupDisabled') !== "true" ) {
        $('#croWrapper').animate({
            bottom: 0
        }, 'fast');
    }
});

$('#croWrapper').on('click', '#xclose', function () {
    sessionStorage.setItem('semCroPopupDisabled', "true");
    $('#croWrapper').hide();
});
});

Upvotes: 1

Views: 536

Answers (1)

jhpratt
jhpratt

Reputation: 7120

Session storage is for a single session, which ends when the browser is closed. What you're looking for is local storage, which has the same API and does not end with the session (browser close)

Upvotes: 2

Related Questions