简体   繁体   中英

JAVASCRIPT: Is that possible to avoid window to close with event beforeunload?

是否可以取消javascript中的关闭窗口(无论是否通过mootools),如下所示?

window.addEventListener("beforeunload",function(){doSomething(); return false;})

Yes. You use onbeforeunload and return the string that you want prompted. The browser handles all the work from there for obvious reasons. If the user is okay to leave simply return nothing.

window.onbeforeunload = function() 
{
   if(...) {
      return 'Your changes have NOT been saved.';
   }

   return;

}

Thank goodness no.

You can however give the user a confirm box just in case they have unsaved changes or some such thing.

window.onbeforeunload = function() {
    return 'You have unsaved changes.'
};

Don't annoy your users by trying to keep them on your page. It's only going to backfire, and cause people to actively dislike your site.

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