添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
jQuery issues have moved to GitHub . This site is now a static archive of the old Trac bugs site. Some functions and pages are no longer available. Skip to main content
Description

Moved from: http://bugs.jqueryui.com/ticket/8439

No matter what $(window).beforeunload(fn) does, it cannot interact reliably with a mix of native inline functions. For other events, jQuery attaches a private native handler via addEventListener or attachEvent and manages jQuery handlers in its own data structures. Here we are all trying to share a single window.onbeforeunload property, and it can't work.

Scenarios:

   window.onbeforeunload = fn1;
   $(window).on("beforeunload", fn2);
   $(window).on("beforeunload", fn3);

Currently, we clobber fn1 and it is never called; only fn2 and fn3 would be called.

   window.onbeforeunload = fn1;
   $(window).on("beforeunload", fn2);
   $(window).on("beforeunload", fn3);
   window.onbeforeunload = null;

If the code that sets fn1 later tries to remove it by null ing out the handler, fn2 and fn3 don't run.

Having jQuery and the native code save any existing handler allows everyone to run, but prevents anyone from removing their handler; perhaps that is less of a problem in real-life scenarios.

There is also the return value problem. Unlike other event handlers that return false as a flag, the onbeforeunload event returns a **string** that is supposed to be displayed to the user, such as "Are you sure you want to leave this page? Your edits will be lost". In the case that there are multiple handlers what should be displayed to the user? Right now, we display the string from the *last* jQuery handler called.

Hello

Recently I needed to make a queue that is updated via ajax.

It is updated recursively with a interval between each request.

Users complained that they were receiving an error message when accessed any link while the queue is updated.

So I needed to identify if the onError was called by a failure or because the user left navigation during the ajax request.

I fulfilled using the combination of $.ajaxError and $.bind('beforeunload', func) in the following code http://jsfiddle.net/7A6Ne/

But I just tested in Chrome 27, IE 10 and Firefox, so I'm afraid because I do not know if implementation of beforeunload is ok in all major browsers.

Should be fine if $.ajaxError tell us if user left navigation.