Majase
Majase

Reputation: 91

prevent bootstrap 3 modal from closing when user refreshes the entire page

How can I prevent bootstrap 3 modal from closing when the user refreshes the page? I want the user to close the modal only using the CLOSE button, nothing else.

Upvotes: 4

Views: 1501

Answers (3)

Nuwan Chamara
Nuwan Chamara

Reputation: 247

you cannot do that.You have to store a value in localstorage/cookie/session.you have to reopen the modal using the value which you store in the local storage/cookie/session . I agree with durga's answer.I think it is the best possible answer.

Upvotes: 1

Chaitanya
Chaitanya

Reputation: 171

Consider opening on page load method based on a flag, here it is isModalOpen

function openModal() {
    $('#myModal').modal('show');
    localStorage.setItem('isModalOpen', true);
};

function closeModal() {
    $('#myModal').modal('hide');
    localStorage.setItem('isModalOpen',false);
};

During page load

if(localStorage.getItem('isModalOpen')) {
    openModal();
}

Upvotes: 2

Robban
Robban

Reputation: 6802

That can't be done. When the user refreshes the page the modal will be gone.

Your only option would be to re-open the modal on page refresh. To do that you would have to store some variable in a cookie or localStorage to tell you that they haven't yet closed the modal.

Then you could do something like this:

$(function() {
    if(cookieOrLocalStorageVariable) {
       $('#myModal').modal(options);
    }
});

The cookieOrLocalStorageVariable would need to be defined by you and should be set once the modal is shown.

Upvotes: 8

Related Questions