如何显示"您确定要离开页面吗?"当用户实际试图关闭页面时(单击浏览器窗口或选项卡上的X按钮),而不是当用户试图离开页面时(单击另一个链接).
My client wants a message to appear when the user tries to close the page "Are you sure you want to leave the page? You still have items in your shopping cart."
不幸的是,$(window).bind('beforeunload')
并不是只有在用户关闭页面时才会触发的.
jQuery:
function checkCart() {
$.ajax({
url : 'index.php?route=module/cart/check',
type : 'POST',
dataType : 'json',
success : function (result) {
if (result) {
$(window).bind('beforeunload', function(){
return 'leave?';
});
}
}
})
}