1

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();
});
});
jhpratt
  • 6,841
  • 16
  • 40
  • 50
Michele B.
  • 11
  • 1
  • https://stackoverflow.com/questions/5523140/html5-local-storage-vs-session-storage – Marek Nov 22 '17 at 18:31
  • Read the documentation before asking questions https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage – charlietfl Nov 22 '17 at 18:35
  • I think Michele is using sessionStorage specifically because she is aware of the difference between it and localStorage, and desires the erased-on-session-end functionality of sessionStorage. Her question indicates, to me, that she is having trouble utilizing that functionality within a given session, not between sessions. – Xeraqu Nov 22 '17 at 18:46

1 Answers1

2

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)

jhpratt
  • 6,841
  • 16
  • 40
  • 50
  • I'll upvote this if you post an example snippet(or even just code) to more clearly define the difference between the two. – zfrisch Nov 22 '17 at 18:33
  • 3
    An example snippet is literally changing `sessionStorage` to `localStorage`, so it's hardly necessary. The difference is as stated - sessionStorage is for a session, which ends upon browser close. Local storage doesn't. Not much more to it than that. – jhpratt Nov 22 '17 at 18:35