2012-02-13 65 views
0

我有一個jsp頁面,如果用戶關閉頁面,彈出框應該出現,詢問他是否真的想關閉頁面。根據用戶選擇關閉jsp頁面

如果他選擇關閉頁面,那麼我需要使用ajax重定向到另一個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(); 

});