I'm not sure whether you want to show it once per session or just once to any user who visits (and not again when they visit next time, even if it's the next day).
If it's just once, ever, then you can store a flag in localStorage
. If it's once per session, you can store a flag in sessionStorage
. Both are extremely well-supported.
Here's a localStorage
example; sessionStorage
is exactly the same except you use sessionStorage
instead of localStorage
:
if (!localStorage.getItem("shown-popup")) {
// We haven't shown it yet
setTimeout(function() {
// ----show the div here ----
// Flag we've shown it (you might do this only when they click the [x])
localStorage.setItem("shown-popup", "yes");
}, 2000);
}
Note that items in local storage are strings.
To allow for the few without local storage or who have it disabled (private browsing, etc.), wrap the check and the set in try/catch
:
var needToShowPopup = true;
try {
needToShowPopup = !localStorage.getItem("shown-popup");
} catch (e) {
}
if (needToShowPopup) {
// We haven't shown it yet
setTimeout(function() {
// ----show the div here ----
// Flag we've shown it (you might do this only when they click the [x])
try {
localStorage.setItem("shown-popup", "yes");
} catch (e) {
}
}, 2000);
}