skip to Main Content

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;
});

2

Answers


  1. You have to delete the cookie on .unload event

    $(window).unload(function() {
        cookieDelete();
    });
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search