On my home page, I have an overlay with a button to enter the website. On click, the overlay disappears. The overlay should only appear once, on first page load. Here is my issue:
My question: why is the overlayd being displayed the second time I enter the page?
Here is my code:
var session = sessionStorage.getItem('session');
console.log(session);
if (session !== null) {
$('.overlay').hide();
}
sessionStorage.setItem('session', 1);
Use the following code. it's set overlay show only once in 24 hours when you visit the website first time.
$(document).ready(function(){
if ($.cookie('overlay_status') != '1') {
setTimeout(function() {
$('.overlay').show();
$.cookie('overlay_status', '1', { expires: 1});
}, 20000);
} else {
$('.overlay').hide();
}
});
This eventually seems to do the trick for me...
function setCookie(key, value, expiry) {
var expires = new Date();
expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
function eraseCookie(key) {
var keyValue = getCookie(key);
setCookie(key, keyValue, '-1');
}
if (getCookie('overlay_status') != '1') {
setTimeout(function () {
$('.overlay').show();
setCookie('overlay_status', '1', '1');
}, 20000);
} else {
$('.overlay').hide();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.