我想捕获浏览器窗口/选项卡关闭事件.我用jQuery尝试了以下内容:
jQuery(window).bind(
"beforeunload",
function() {
return confirm("Do you really want to close?")
}
)
Run Code Online (Sandbox Code Playgroud)
但它也适用于表单提交,这不是我想要的.我想要一个仅在用户关闭窗口时触发的事件.
如何显示"您确定要离开页面吗?" 当用户实际尝试关闭页面时(单击浏览器窗口或选项卡上的X按钮),当他试图离开页面时(单击另一个链接).
当用户尝试关闭页面时,我的客户希望显示一条消息"您确定要离开页面吗?您的购物车中仍有商品."
不幸的是$(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?';
});
}
}
})
}
Run Code Online (Sandbox Code Playgroud)