2012-02-13 3 views
0

사용자가 페이지를 닫으면 실제로 페이지를 닫으려고하는지 묻는 팝업 상자가 나타나는 JSP 페이지가 있습니다.사용자 선택에 따라 jsp 페이지 닫기하기

페이지를 닫으려고하면 아약스를 사용하여 다른 JSP 페이지로 리디렉션해야합니다.

어떻게 동일한 결과를 얻을 수 있습니까?

닫기 버튼을 클릭 할 때 사용자에게 묻는 샘플 코드가 있지만 새로 고침, 제출 또는 선택 취소 버튼 또는 다른 버튼을 누르면이 메시지가 표시됩니다.

누구든지 주어진 코드 또는 동일한 기능을 수행하는 새로운 기능을 수정할 것을 제안 할 수 있습니까?

sample.jsp 
<html> 
<head> 
<title>Refresh a page in jQuery</title> 

<script type="text/javascript" src="jquery-1.4.2.min.js"></script> 

</head> 

<body> 

<h1>Stop a page from exit with jQuery</h1> 

<button id="reload">Refresh a Page in jQuery</button> 

<script type="text/javascript"> 

    $('#reload').click(function() { 

     location.reload(); 

    }); 

    $(window).bind('beforeunload', function(){ 
     return '>>>>>Before You Go<<<<<<<< \n Your custom message go here'; 
    }); 

</script> 

</body> 
</html> 

답변

1
$(window).bind('beforeunload', function(){ 
    return 'Are you sure to leave'; 
}); 

$(window).bind('unload', function(){ 
    $.ajax({ 
     url:'some url', 
     success:function(data){ 
      $("body").html(data); 
     } 

    }) 
}) 

당신은 새로 고침 이전에 이벤트를 바인딩을 해제해야

$('#reload').click(function() { 
    $(window).unbind("beforeunload"); 
    $(window).unbind("unload"); 
    location.reload(); 

}); 
관련 문제