How can I override the OnBeforeUnload dialog and replace it with my own?

前端 未结 11 2028
一生所求
一生所求 2020-11-21 06:14

I need to warn users about unsaved changes before they leave a page (a pretty common problem).

window.onbeforeunload=handler

This works bu

相关标签:
11条回答
  • 2020-11-21 07:08

    You can detect which button (ok or cancel) pressed by user, because the onunload function called only when the user choise leaveing the page. Althoug in this funcion the possibilities is limited, because the DOM is being collapsed. You can run javascript, but the ajax POST doesn't do anything therefore you can't use this methode for automatic logout. But there is a solution for that. The window.open('logout.php') executed in the onunload funcion, so the user will logged out with a new window opening.

    function onunload = (){
        window.open('logout.php');
    }
    

    This code called when user leave the page or close the active window and user logged out by 'logout.php'. The new window close immediately when logout php consist of code:

    window.close();
    
    0 讨论(0)
  • 2020-11-21 07:09

    Angular 9 approach:

    constructor() {
        window.addEventListener('beforeunload', (event: BeforeUnloadEvent) => {
         if (this.generatedBarcodeIndex) {
          event.preventDefault(); // for Firefox
          event.returnValue = ''; // for Chrome
          return '';
         }
         return false;
        });
       }
    

    Browsers support and the removal of the custom message:

    • Chrome removed support for the custom message in ver 51 min
    • Opera removed support for the custom message in ver 38 min
    • Firefox removed support for the custom message in ver 44.0 min
    • Safari removed support for the custom message in ver 9.1 min
    0 讨论(0)
  • 2020-11-21 07:11

    Try placing a return; instead of a message.. this is working most browsers for me. (This only really prevents dialog's presents)

    window.onbeforeunload = function(evt) {            
            //Your Extra Code
            return;
    }
    
    0 讨论(0)
  • 2020-11-21 07:11
     <script type="text/javascript">
            window.onbeforeunload = function(evt) {
                var message = 'Are you sure you want to leave?';
                if (typeof evt == 'undefined') {
                    evt = window.event;
                }       
                if (evt) {
                    evt.returnValue = message;
                }
                return message;
            } 
        </script>
    

    refer from http://www.codeprojectdownload.com

    0 讨论(0)
  • 2020-11-21 07:19

    1) Use onbeforeunload, not onunload.

    2) The important thing is to avoid executing a return statement. I don't mean, by this, to avoid returning from your handler. You return all right, but you do it by ensuring that you reach the end of the function and DO NOT execute a return statement. Under these conditions the built-in standard dialog does not occur.

    3) You can, if you use onbeforeunload, run an ajax call in your unbeforeunload handler to tidy up on the server, but it must be a synchronous one, and you have to wait for and handle the reply in your onbeforeunload handler (still respecting condition (2) above). I do this and it works fine. If you do a synchronous ajax call, everything is held up until the response comes back. If you do an asynchronous one, thinking that you don't care about the reply from the server, the page unload continues and your ajax call is aborted by this process - including a remote script if it's running.

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