试图检测浏览器关闭事件我尝试过许多方法来通过jQuery或JavaScript检测浏览器关闭事件。但是,不幸的是,我没有能够发现接近。这个onbeforeunload和onunload方法也不起作用。怎样才能侦测到窗户?close, unload,或beforeunload事件?
3 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
window.onbeforeunload = function (event) { var message = 'Important: Please click on \'Save\' button to leave this page.'; if (typeof event == 'undefined') { event = window.event; } if (event) { event.returnValue = message; } return message;};$(function () { $("a").not('#lnkLogOut').click(function () { window.onbeforeunload = null; }); $(".btn").click(function () { window.onbeforeunload = null;});});
#lnkLogOut
.btn
森栏
TA贡献1810条经验 获得超5个赞
$(window).on('mouseover', (function () { window.onbeforeunload = null;}));$(window).on('mouseout', (function () { window.onbeforeunload = ConfirmLeave;}));function ConfirmLeave() { return "";}var prevKey="";$(document).keydown(function (e) { if (e.key=="F5") { window.onbeforeunload = ConfirmLeave; } else if (e.key.toUpperCase() == "W" && prevKey == "CONTROL") { window.onbeforeunload = ConfirmLeave; } else if (e.key.toUpperCase() == "R" && prevKey == "CONTROL") { window.onbeforeunload = ConfirmLeave; } else if (e.key.toUpperCase() == "F4" && (prevKey == "ALT" || prevKey == "CONTROL")) { window.onbeforeunload = ConfirmLeave; } prevKey = e.key.toUpperCase();});
添加回答
举报
0/150
提交
取消