Program Tip

window.onbeforeunload 및 window.onunload가 Firefox, Safari, Opera에서 작동하지 않습니까?

programtip 2020. 10. 23. 08:19
반응형

window.onbeforeunload 및 window.onunload가 Firefox, Safari, Opera에서 작동하지 않습니까?


채팅 응용 프로그램에서 응용 프로그램이 종료되면 사용자로부터 확인을 받아야합니다.

그래서 window.onbeforeunload확인 경고와 window.onunloadlogout ()에 사용했습니다.

  1. 그러나 두 기능 모두 IE와 Chrome에서 작동합니다. (응용 프로그램은 정상적으로 작동합니다)

  2. window.onbeforeunload Opera에서 작동하지 않고 내 메시지가 Firefox에 표시되지 않습니다.

  3. window.onunload Safari, Opera 및 Firefox에서 작동하지 않습니다.

내 자바 스크립트 코드는,

// Used for confirmation , to closing the window 
window.onbeforeunload = function () {

    return  "Are you sure want to LOGOUT the session ?";
}; 

// Used to logout the session , when browser window was closed 
window.onunload = function () {

    if((sessionId != null)&&(sessionId!="null")&& (sessionId != ""))
        logout();
};

또한 JQuery로 동일한 기능을 시도했습니다.

<script type="text/javascript">

    $(window).on('beforeunload', function() {
        return 'Are you sure want to LOGOUT the session ?';
    });

    $(window).unload(function() {
        if ((sessionId != null) && (sessionId != "null") && (sessionId != "")) {
            logout();
        }
    });

</script>

안타깝게도 사용중인 방법은 해당 브라우저에서 지원되지 않습니다. 내 대답을 뒷받침하기 위해 (이 비지 지적인 행동) 아래 링크를 제공했습니다.

onbeforeunload그리고 이것을 지원하기 위해 ...에서 onunload일하지 않습니다.opera

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

하지만 onunload이벤트가 완전하게 작동하지 않습니다, 당신이 사용할 수있는 onunload사용자가 저장되지 않은 양식 페이지에서 다른 페이지로 이동하는 링크를 클릭하면 경고를 표시 할 수 있습니다.

onunloadsafari...에서 작동하지 않습니다.

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

pagehide대신 사파리 브라우저에서 이벤트를 사용해 볼 수 onunload있습니다.

onunloadfirefox...에서 작동하지 않습니다.

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

그들은 아직 FF에서도 해결책을 찾지 못했습니다.

행운을 빕니다.


즉, firefox 및 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;
            });

나는 jQuery와 함께 IE와 FF에서 작동하도록 할 수 있었다.

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

});

대신 : unload, onunload 또는 onbeforeunload


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.


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;
};

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

참고URL : https://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in-firefox-safari-o

반응형