如何使用javascript或jquery识别浏览器选项卡关闭?
问题描述:
如何使用JavaScript或jquery识别浏览器选项卡关闭?当用户单击浏览器关闭按钮时,我必须给出一条确认消息.如果用户按是"按钮,则必须在同一选项卡上加载另一页.如果按取消.然后关闭标签.任何人都知道此功能的脚本.请帮助我:(
How to identify browser tab closing using javascript or jquery? I have to give a confirmation message when user click on the browser close button. If user press yes button I have to load another page on the same tab. if press cancel. then close the tab. any body knows the scripts for this functionality. Please help me :(
答
尝试如下操作:
$(window).unload(function() {
var bye = confirm("Go away?");
if (bye) {
// do stuff
window.location = "http://somewhere_else";
} else {
// do stuff?
// stay here
}
});