문제

이전에 전달 된 핸들러가 설치된 팝업 창이 만들어졌습니다. "Close"파일 메뉴 항목이 팝업을 닫는 데 사용되면, 전후의 핸들러가 두 번 호출되어 두 개의 "이 창을 닫으시겠습니까?" 메시지가 나타납니다.

이것은 Firefox가있는 버그입니다 여기에보고했습니다, 그러나 나는 여전히 이런 일이 일어나지 않도록하는 방법을 원합니다. 이중 메시지 문제를 방지하기 위해로드 전에 두 배를 감지하는 제정신 방법을 생각할 수 있습니까? 문제는 Firefox가 사용자가 클릭하거나 확인하거나 취소하기로 선택한 대화 상자의 어떤 버튼을 알려주지 않는다는 것입니다.

도움이 되었습니까?

해결책 3

이것은 확실히 FF 버그입니다. 나는 그것을보고했다 https://bugzilla.mozilla.org/show_bug.cgi?id=531199

다른 팁

<script type="text/javascript">
var onBeforeUnloadFired = false;

window.onbeforeunload = function ()
{
    if (!onBeforeUnloadFired) {
        onBeforeUnloadFired = true;
        event.returnValue = "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
   }

   window.setTimeout("ResetOnBeforeUnloadFired()", 10);
}

function ResetOnBeforeUnloadFired() {
   onBeforeUnloadFired = false;
}    
</script>

대화 상자가 두 번째로 나타나는 것을 방지하기 위해 핸들러의 변수를 설정하십시오. Settimeout을 사용하여 나중에 재설정하십시오.

내가 찾은 가장 좋은 솔루션은 500 밀리 초 후에 재설정되는 플래그 글로벌 변수를 사용하는 것입니다 (이는 함수가 다시 호출 될 수 있지만 외관 직후에 다시 호출되지 않도록합니다).

마지막 코드를 참조하십시오.

http://social.msdn.microsoft.com/forums/en/sharepointinfopath/thread/13000cd8-5c50-4260-a0d2-c404764966d

Chrome 21, Firefox 14, IE 7-9, Safari 5 (PC) 에서이 문제를 발견했습니다.

다음은 이러한 모든 브라우저에서 작동합니다. 이벤트 중에 window.neforeunload 함수를 제거하면 두 번째 호출이 방지됩니다. 속임수는 Window를 재설정하는 것입니다.

var window_on_before_unload = function(e) {
    var msg;
    // Do here what you ever you need to do
    msg = "Message for user";

    // Prevent next "window.onbeforeunload" from re-running this code.
    // Ensure that if the user decides to stay on the page that
    // this code is run the next time the user tries to leave the page.
    window.onbeforeunload = set_on_before_unload;

    // Prepare message for user
    if (msg) {
        if (/irefox\/([4-9]|1\d+)/.test(navigator.userAgent))
            alert(msg
                    + '\n\nThe next dialog will allow you to stay here or continue\nSee Firefox bug #588292');

        (e = e || window.event).returnValue = msg;
        return msg;
    }
};

// Set window.onbeforeunload to the above handler.
// @uses window_on_before_unload
// @param {Event} e
var set_on_before_unload = function(e) {
    // Initialize the handler for window.onbeforeunload.
    window.onbeforeunload = window_on_before_unload;
}

// Initialize the handler for window.onbeforeunload.
set_on_before_unload();

핸들러 내부에서 true로 설정된 전역 변수를 만듭니다. 이 변수가 False 일 때 경고/팝업 만 표시하십시오.

다음 스 니펫을 사용하여 ExitCount를 추적합니다

페이지가로드되면 다음 변수 ExitCount가 초기화됩니다.

if (typeof(MTG) == 'undefined') MTG = {};
MTG.exitCount=0; 

그리고 Window 언로드 이벤트에서

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


            if (MTG.exitCount<=0) 
            {

                             //do your thing, save etc
            }   
            MTG.exitCount++;




});

확인 ()를 확인하기 위해 자신의 전화를하는 대신. 전직 행사 내에서. Firefox는 자체 확인 대화 상자를 던졌습니다. 이것이 올바른/표준 일인지 확실하지 않지만 그것이 그들이하는 방식입니다.

Window.open이있는 다른 팝업 창을 여는 문서가 있습니다. 원래 창에서 나는 다음과 같은 "언로드"이벤트에 대한 리스너를 (jQuery와 함께) 등록했습니다.

var popup_window = window.open(...) 
$(popup_window).on('unload', function(event) ...

이벤트가 효과적으로 두 번 트리거 되었기 때문에이 페이지를 발견했습니다. 내가 찾은 것은 버그가 아니라는 것입니다. "about : blank"페이지가 페이지로 대체되고 다른 페이지가 언로드되는 페이지로 한 번 발사되기 때문에 두 번 트리거합니다.

내가해야 할 일은 원래 이벤트를 쿼리하여 관심있는 이벤트를 필터링하는 것입니다.

function (event) {
   var original_url = e.originalEvent.originalTarget.URL;
   if (original_url != 'about:blank')
   {
       ... do cool things ...
   }
}

이것이 원래 질문에 적용되는지는 모르겠습니다. 왜냐하면 창문이 다른 창을 열는 특별한 경우이기 때문입니다. 그러나 도움이되기를 바랍니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top