javascript confirm dialog box before close browser window

前端 未结 3 1022
日久生厌
日久生厌 2021-01-05 14:02

I need to display a confirm dialog box before closing the browser window using javascript or PHP. The confirm box should come up when I click the close button of browser; ot

相关标签:
3条回答
  • 2021-01-05 14:39

    This will display it when closing the 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;
    }
    
    0 讨论(0)
  • 2021-01-05 14:46

    With Jquery:

    $(window).bind('beforeunload', function(e) {
        // Your code and validation
        if (confirm) {
            return "Are you sure?";
        }
    });
    
    0 讨论(0)
  • 2021-01-05 14:48

    Use this code , I used that earlier, I 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>
    
    0 讨论(0)
提交回复
热议问题