2015-01-01 3 views
0

내 JQuery에서 페이지를 떠나는 사용자에게 작용하는 두 가지 기능이 있습니다. 사용자가 브라우저를 닫아 페이지를 떠나면 생각은, 우리가 사용됩니다 :window.unload 이벤트를 중지하는 방법

$(window).bind('beforeunload', function() { 
    return "Are you sure you want to leave this page without saving or submitting?"; 
}); 

지금, 우리는 그들이 페이지에서 로그 아웃 경우에만 진행 상황을 저장할 것인지를 사용자에게 물어보고 싶은, 사용 : 이제

$("#logoutButton").click(function(e) { 
    var result = confirm("Would you like to save before logging out?"); 

    if(result == true) { 
    $("#savebutton").click(); //simulate a click on save before redirecting 
    } 

    window.location.href = "www.redirectPage.com"; //go to logout page regardless of choice 
}); 

, 내가 직면하고있는 문제점은, 사용자가 클릭 로그 아웃 할 때마다, 그것은이 제대로 확인 저장 수행하는 것입니다,하지만 그들은 페이지를 마칠 경우 여전히 beforeunload를 사용하여, 사용자에게 요청 방법. 이 경우에 beforeunload이 로그 아웃 버튼 click 기능 내부에서 트리거되는 것을 막을 수 있습니까?

+3

그냥 변경하기 전에 ($ (창) .unbind ('beforeunload')''로) 이벤트 핸들러 바인딩을 해제' location.href'. – raina77ow

답변

1

.unbind()을 사용하면 특정 이벤트에 대한 모든 이벤트 처리기를 제거 할 수 있습니다. 당신의 코드에서, 그 바로 저장 한 후

$(window).unbind('beforeunload'); 

가 호출 될 것이다 :

$("#logoutButton").click(function(e) { 
    var result = confirm("Would you like to save before logging out?"); 

    if(result == true) { 
     $("#savebutton").click(); //simulate a click on save before redirecting 
     $(window).unbind('beforeunload'); 
    } 

    window.location.href = "www.redirectPage.com"; //go to logout page regardless of choice 
}); 
관련 문제