I wrote this simple code for closing an span after clicking on X, but It loads every time I refresh page or go on another page.
function hinweisClose() {
var x = document.getElementById("hinweis_header"); {
x.style.display = "none";
}
}
And this is the code in php:
<div id="hinweis_header">
<span style="vertical-align: middle; padding-top: 20px; font-weight: bold;"><a href="<?=SHOP_URL_HTTPS?>/<?=$param["links"]["service"]?>/<?=$sprachdatei["links"]["link_hinweis"]?>"><b>Nur heute: 10 % auf alle Jersey Stoffe >><?=$sprachdatei['header']['hinweis_header']?></b></a></span>
<div class="close-btn" style="position: absolute; display: inline-block; top: -4px; padding-right: 6px; color: #fff;"><span class="rwd-buttinette-icon rwd-icon-remove-circle-1" onclick="hinweisClose()" style="float: right;"></span></div>
</div>
And I've tried with sessionStorage, but can't get it to work:
if (sessionStorage.getItem('HinweisOnce') !== 'true') {
function hinweisClose() {
var x = document.getElementById("hinweis_header"); {
x.style.display = "none";
}
}
sessionStorage.setItem('HinweisOnce','true');
}
I want it to load just once until it's closed and stays closed for that session. Can someone help me?
After closing the span add a cookie, for example:
document.cookie = "popup=closed";
Then you can check for the cookie before displaying the span again, for example:
isClosed = document.cookie.replace(/(?:(?:^|.*;\s*)popup\s*\=\s*([^;]*).*$)|^.*$/, "$1") === "closed";
if(isClosed) {
displayMySpan();
}
You can read more about JS cookies here.
For PHP based solution you can still set the cookie after closing the span, something like:
document.cookie = "popup=closed; path=/";
Then in your PHP code you need something like:
if (isset($_COOKIE['popup']) && $_COOKIE['popup'] === "closed") {
//code to display div here
};