Javascript confirmation dialog before closing browser window

I need to display a confirmation dialog before closing the browser window using javascript or PHP. A confirmation box should appear when I click the close button of the browser; otherwise, the dialog should not be displayed.

thanks

+6
source share
3 answers

This will display it when you close your browser:

window.onbeforeunload = function (event) { var message = 'Sure you want to leave?'; if (typeof event == 'undefined') { event = window.event; } if (event) { event.returnValue = message; } return message; } 
+12
source

Use this code, I used it earlier, <here

 <html> <head> <title>.:I 0wn U:.</title> <script language="JavaScript"> <!-- window.onbeforeunload = bunload; function bunload(){ dontleave="Are you sure you want to leave?"; return dontleave; } //--> </script> </head> <body> Please stay on this page! </body> </html> 
+4
source

Using jQuery:

 $(window).bind('beforeunload', function(e) { // Your code and validation if (confirm) { return "Are you sure?"; } }); 
+1
source

Source: https://habr.com/ru/post/894438/


All Articles