如果你想在用户离开页面之前确认是否离开,最好使用beforeunload事件在beforeunload事件里,不需要alert,该事件会自动产生一个弹出框,
就算你加了alert也是没效果的。
另外,要使beforeunload事件能自动弹出框,必须得有返回值,如:
JavaScript code
$(window).on('beforeunload', function(event) {
var message = 'I’m really going to miss you if you go.';
event.returnValue = message;
return message;
});