when the site opens, I open a modal . When the site is closed, I want to delete the cookies. I tried the codes below, but when the page is refreshed, the modal opens again. How can I delete cookies only when the site is closed. thanks
$(document).ready(function () {
if ($.cookie("announcement") == null) {
$.cookie("announcement", "yes");
$("#large").modal("show");
}
});
function cookieDelete() {
$.removeCookie("announcement");
}
$(window).on("beforeunload", function () {
cookieDelete();
return;
});
CodePudding user response:
If Cookie is not a requirement, I suggest an alternate solution using Session Storage
$(document).ready(function () {
const announcement = sessionStorage.getItem("announcement");
if (!announcement) {
sessionStorage.setItem("announcement", "yes");
$("#large").modal("show");
}
});
You don't need to handle delete
operation because session storage
will be cleared automatically when the page is closed.
CodePudding user response:
You have to delete the cookie on .unload event
$(window).unload(function() {
cookieDelete();
});