Setting up a privacy policy notification bar and looking to have it displayed only once per user or session and only on the front page or landing page. Right now, it is showing up on every page for every visit.
html
<span class="banner tracking-banner p-t-1">
<div class="container">
<div class="row">
<div class="col-sm-10 text-left m-b-1">
This website uses cookies and other 3rd party services to customize and provide you a more personalized experience. To find out more, see our <a href="/privacy/">Privacy Policy</a>.
</div>
<div class="col-sm-2 m-b-1">
<button class="dismiss btn btn-sm btn-block btn-invert">Accept</button>
</div>
</div>
</div>
</span>
js
$(document).ready(function(){
$(".banner").fadeIn("slow").append("");
$(".dismiss").click(function(){
$(".banner").fadeOut("fast");
});
})
is there a way to add user or session tracking so that the notification is not showing up on every page every time?
See session storage: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
Or local storage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
Example:
$(document).ready(function() {
if (!sessionStorage.getItem('policy')) {
$(".banner").fadeIn("slow").append("");
$(".dismiss").click(function() {
$(".banner").fadeOut("fast");
sessionStorage.setItem('policy', 'read');
});
}
});
Use sessionStorage to temporary save the state of showing alert:
$(document).ready(function(){
if (sessionStorage.alertshown!="yes"){
$(".banner").fadeIn("slow").append("");
$(".dismiss").click(function(){
$(".banner").fadeOut("fast");
sessionStorage.alertshown="yes";
});
}
})
A couple things to tackle here:
$(document).ready(function(){ if(!sessionStorage.alertShown) { $(".banner").fadeIn("slow").append(""); $(".dismiss").click(() => $(".banner").fadeOut("fast")); sessionStorage.alertShown = true; //set to true so they aren't shown again } });
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.