문제

더러운 플래그가 설정되면 페이지를 떠나고 싶다면 사용자에게 확인하고 싶은 시스템이 있습니다.

다음 코드를 사용하고 있습니다. Firefox에서는 FireBug를 통해 페이지 소스를 볼 수 있으며 태그에는 onbeforeunload 속성이 삽입되었습니다.

Chrome과 Firefox에서는 이런 일이 발생하지 않으며 경고없이 페이지에서 벗어날 수 있습니다. 바디 태그를 업데이트하기위한 jQuery 라인은 확실히 실행되고 있으며, 그냥 수행하지 않습니다.

if ($("body").attr('onbeforeunload') == null) {
    if (window.event) {
        // IE and Chrome use this
        $("body").attr('onbeforeunload', 'CatchLeavePage(event)');
    }
    else {
        // Firefox uses this
        $("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
    }
}

여기에서 진행하는 방법이 있습니까?

도움이 되었습니까?

해결책

False를 반환하여 페이지 언로드를 중단 할 수 없습니다. 메시지 상자에서 사용자에게 표시되는 문자열을 반환해야하며 페이지에 떠나거나 머무를지 결정합니다 ( 'OK'또는 'CANCEL'버튼을 선택하여). 이것:

 window.onbeforeunload = function() {
    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
 };

다른 팁

이 시도

  <script type=\"text/javascript\">
        var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
        var leave_message = 'You sure you want to leave?'
        function goodbye(e) 
        {
            if(dont_confirm_leave!==1)
            {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) 
                {
                    e.stopPropagation();
                    e.preventDefault();
                }

                //return works for Chrome and Safari
                return leave_message;
            }
        }   
    window.onbeforeunload=goodbye;
    </script>
window.onbeforeunload = function () { return 'Are you sure?' };

이 코드 확인 :

var validNavigation = false;

function wireUpEvents() {
var dont_confirm_leave = 0; 
var leave_message = "You sure you want to leave ?";

function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}

window.onbeforeunload = goodbye;

document.onkeydown = function () {
switch (event.keyCode || e.which) {
case 116 : //F5 button
validNavigation = true;
case 114 : //F5 button
validNavigation = true;
case 82 : //R button
if (event.ctrlKey) {
validNavigation = true;
}
case 13 : //Press enter
validNavigation = true;
}

}
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});

// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});

// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});

예쁘지는 않지만 트릭을했습니다.

var warnclose = true;
var warn = function(e) {
    var warning = 'Your warning message.';
    if (warnclose) {
        // Disables multiple calls
        warnclose = false;

        // In case we still need warn to be called again 
        setTimeout(function(){
            warnclose = true;
        }, 500);

        return warning;
    }
};
window.onbeforeunload = warn;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top