Event calling before page unload

后端 未结 1 1250
南旧
南旧 2020-12-09 18:53

I want to pop up a dialog before an html page is unloaded asking to unload page or not. If yes is selected then the unload continues, and if no is selected the

相关标签:
1条回答
  • 2020-12-09 19:14

    You can't set the text to "yes"/"no" (it'll be "Ok"/"Cancel" in most browsers), but you can prompt (be careful when using this) when leaving the page in any manner using window.onbeforeunload, like this:

    window.onbeforeunload = function() {
      return "Are you sure you wish to leave the page?";
    }
    

    Only use this if you have a good reason, don't annoy your users. That being said, this will prompt when leaving the page, that includes history back, forward, refresh, closing the window/tab, clicking a link to navigate away or submitting a form, etc. So you'll probably want to call this when going somewhere that shouldn't prompt:

    window.onbeforeunload = null;
    

    For example, submitting a form of information, you'd want to prompt when leaving/losing the data, but not when actually submitting it.

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