Can I pop up a confirmation dialog when the user is closing the window in Safari/Chrome?

后端 未结 4 1323
遥遥无期
遥遥无期 2020-11-29 12:33

In IE and FF, i can attach an event handler to onBeforeUnload, and by passing a string to a property of the event, the user will see a dialog asking him whether he wants to

相关标签:
4条回答
  • 2020-11-29 12:58

    This question is covered in slightly more detail in another newer StackOverFlow question: Setting onbeforeunload on body element in Chrome and IE using jQuery

    0 讨论(0)
  • 2020-11-29 13:02

    StackOverflow itself uses onbeforeunload, and it works fine for me in Safari:

    function setConfirmUnload(a){window.onbeforeunload=a?function(){return a}:null}
    
    0 讨论(0)
  • 2020-11-29 13:14

    I checked it in chrome and it seems to be working fine using this:

    <body onunload="alert('x');" onbeforeunload="test1();">
    
    0 讨论(0)
  • 2020-11-29 13:16

    This works perfectly for me in both Chrome and Safari:

    <html><body><p>Test</p>
    <script>window.onbeforeunload = function() { return "Sure?"; }</script>
    </body></html>
    

    When I try to close the window, I get the prompt.

    0 讨论(0)
提交回复
热议问题