简体   繁体   中英

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

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?

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?";
};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM