beforeunload event

The beforeunload event is fired when the window, the document and its resources are about to be unloaded.

目前该事件只有IE支持自定义消息,Chrome, Safari, Firefox都已不再支持(因为很多诈骗网站用此消息欺骗用户的原因)。

可以用此事件来防止backspace引发的意外的退回上一页,也可以用来提醒用户离开页面前保存表单。

用jQuery绑定此事件:

$(window).on('beforeunload', function(){
      return 'Are you sure you want to leave?'; // return anything will trigger this event, comment return statement, this event won't be triggered.
});

用jQuery解除此事件的绑定:

$(window).off('beforeunload');

 

参考链接:

https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en#remove_custom_messages_in_onbeforeunload_dialogs

https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

posted @ 2016-11-24 19:37  liangzi4000  阅读(331)  评论(0编辑  收藏  举报