开发者

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

开发者 https://www.devze.com 2023-01-24 01:42 出处:网络
Is that possible to cancel a window close in javascript (via mootools or not) w开发者_C百科ith something like below?

Is that possible to cancel a window close in javascript (via mootools or not) w开发者_C百科ith something like below?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消