Home > Enterprise >  How to make a confirm box appear when user closes or refreshes page
How to make a confirm box appear when user closes or refreshes page

Time:10-03

so I'm making a drawing app. the problem is, when the user accidentally closes or refreshes the page, all the drawings will be lost. So I made a confirmation box that will confirm if the user really wants to close the page.

I have tried like this:

if (window.onbeforeunload == true ) {
confirm("Change are still not saved. Are you sure?")
}

But it doesn't work. Then what is the best solution to solve this problem?

CodePudding user response:

onbeforeunload is a function which is called when leaving the page. So you have to define it.

window.onbeforeunload = function()
{
    return "Change are still not saved. Are you sure?";
};
  • Related