[removed] and [removed] is not working in Firefox , Safari , Opera?

后端 未结 6 1839
心在旅途
心在旅途 2020-11-22 12:11

In my chat application I need to get confirmation from user , when my application closes.

So I used the window.onbeforeunload for confirmation alert and

相关标签:
6条回答
  • 2020-11-22 12:30

    The onunload event is not called in all browsers. Worse, you cannot check the return value of onbeforeunload event. That prevents us from actually preforming a logout function.

    However, you can hack around this.

    Call logout first thing in the onbeforeunload event. then prompt the user. If the user cancels their logout, automatically login them back in, by using the onfocus event. Kinda backwards, but I think it should work.

    'use strict';
    
    var reconnect = false;
    
    window.onfocus = function () {
      if (reconnect) {
        reconnect = false;
        alert("Perform an auto-login here!");
      }
    };
    
    window.onbeforeunload = function () {
      //logout();
      var msg = "Are you sure you want to leave?";
      reconnect = true;
      return msg;
    };
    
    0 讨论(0)
  • 2020-11-22 12:34

    I got the solution for onunload in all browsers except Opera by changing the Ajax asynchronous request into synchronous request.

    xmlhttp.open("POST","LogoutAction",false);
    

    It works well for all browsers except Opera.

    0 讨论(0)
  • 2020-11-22 12:45

    Here is the working solution for ie, firefox and chrome:

    var myEvent = window.attachEvent || window.addEventListener;
    var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
    
                myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
                    var confirmationMessage = 'Are you sure to leave the page?';  // a space
                    (e || window.event).returnValue = confirmationMessage;
                    return confirmationMessage;
                });
    
    0 讨论(0)
  • 2020-11-22 12:46

    Firefox simply does not show custom onbeforeunload messages. Mozilla say they are protecing end users from malicious sites that might show misleading text.

    0 讨论(0)
  • Unfortunately, the methods you are using are unsupported in those browsers. To support my answer (this unsupportive behaviour) I have given links below.

    onbeforeunload and onunload not working in opera... to support this

    onbeforeunload in Opera
    http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/

    Though the onunload event doesn't work completely, you can use onunload to show a warning if a user clicks a link to navigate away from a page with an unsaved form.

    onunload not working in safari... to support this

    https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/

    You could rather try using the pagehide event in the safari browser in lieu of onunload.

    onunload not working in firefox... to support this

    https://bugzilla.mozilla.org/show_bug.cgi?id=681636

    They are yet to come up with a solution in FF too

    Wish you good luck cheers.

    0 讨论(0)
  • 2020-11-22 12:52

    I was able to get it to work in IE and FF with jQuery's:

    $(window).bind('beforeunload', function(){
    
    });
    

    instead of: unload, onunload, or onbeforeunload

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