2013-05-13 13 views
2

이 코드를 실행하면 Internet Explorer에 '정말 가고 싶니?'라는 메시지가 표시됩니다. dirtyMessage가 null 인 경우에도 매번 메시지 내가 도대체 ​​뭘 잘못하고있는 겁니까?Internet Explorer에서 null을 반환하는 경우에도 jquery beforeunload가 메시지를 표시하는 이유

$(window).bind('beforeunload', function() { 
    var dirty = didUserUpdateSomething(); 

    if (dirty) { 
    return "You will lose your changes."; 
    } 
    return null; 
}); 

답변

4

분명히 null을 반환하는 것이 문제입니다. 좋은 이전 IE를 제외한 다른 모든 브라우저에서 작동합니다. 이 솔루션은 (정의) 아무것도 반환되지 않습니다 IE의 beforeunload 조금 황당

$(window).bind('beforeunload', function() { 
    var dirty = didUserUpdateSomething(); 

    if (dirty) { 
    return "You will lose your changes."; 
    } 
    // notice that in case of no dirty message we do not return anything. 
    // having 'return null;' will make IE show a popup with 'null'. 
}); 
0

을; 이벤트 처리기 (심지어 null)에서 ANYTHING을 반환하면 사용자가 페이지를 떠나기를 원한다는 메시지가 나타나면 사용자에게 묻는 상자가 팝업됩니다.

당신은 가능한 해결 방법으로 이것을 시도 할 수 있습니다 : 참조

var beforeUnloadFired = false; 
$(window).bind('beforeunload', function (event) { 
    if (!beforeUnloadFired) { 
     beforeUnloadFired = true; 
     event.returnValue = "You have attempted to leave this page. Are you sure you want to navigate away from this page?"; 
    } 
    window.setTimeout(function() { 
     ResetUnloadFired(); 
    }, 10); 
}); 

function ResetUnloadFired() { 
    beforeUnloadFired = false; 
} 

커플 : https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/beforeunload?redirectlocale=en-US&redirectslug=Mozilla_event_reference%2Fbeforeunload

http://msdn.microsoft.com/en-us/library/ie/ms536907(v=vs.85).aspx

참고 : 내 관련 정보는 "주의"를 참조하십시오 그 두 번째 참조.

+0

'didUserUpdateSomething();'함수에 대해 리팩터링 할 수 있습니다. –

관련 문제