By placing a cookie on the user's browser, you can track if the page has been loaded before based on the existence of the cookie. Once the user clicks on the 'x' or the 'I agree!' link, the message will no longer be displayed.
To ensure the message doesn't reappear, you can simply refresh the page.
If you wish to see the message again, you need to delete the cookie. For this purpose, I have included a button that allows you to delete the cookie and reload the page. Clicking on "Click for delete cookie" will make the cookie disappear and bring back the message.
<!DOCTYPE html>
<head>
<title>Stack Overflow</title>
<body onload="checkBtn()">
<div id="cookieConsent" style="display:none">
<div id="closeCookieConsent" onClick=javascript:addBtn();>x</div>
This website is using cookies. <a href="files/c11bg_cookie_policy.pdf" target="_blank">More info</a>. <a href="#" onClick=javascript:addBtn(); class="cookieConsentOK">I agree!</a>
</div>
<br/>
<br/>
<br/>
<br/>
<div id=btnHolder ><input type="button" onClick=javascript:delCookie("btn"); value="Click for delete cookie" /></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function addBtn(){
if(getCookie("btn"))
document.getElementById('cookieConsent').style.display = 'none';
document.getElementById('cookieConsent').style.display = 'none';
setCookie("btn",true,5);
}
function checkBtn(){
if(!getCookie("btn"))
document.getElementById('cookieConsent').style.display = '';
}
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname){
return (document.cookie.match('(^|; )'+ cname +'=([^;]*)')||0)[2]
}
function delCookie(cname) {
document.cookie = cname+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
window.location.reload();
}
</script>
</body>
I had to comment out the jQuery part as it was causing an error.