diff --git a/user/src/com/google/gwt/user/client/Window.java b/user/src/com/google/gwt/user/client/Window.java index 2fb2789f48..972f16d310 100644 --- a/user/src/com/google/gwt/user/client/Window.java +++ b/user/src/com/google/gwt/user/client/Window.java @@ -506,6 +506,7 @@ public HandlerManager getHandlers() { // Package protected for testing. static WindowHandlers handlers; private static boolean closeHandlersInitialized; + private static boolean beforeCloseHandlersInitialized; private static boolean scrollHandlersInitialized; private static boolean resizeHandlersInitialized; private static int lastResizeWidth; @@ -518,7 +519,10 @@ public HandlerManager getHandlers() { * * @param handler the handler * @return returns the handler registration + * @deprecated This method requires the use of the {@code unload} browser event, which is + * deprecated in all browsers. */ + @Deprecated public static HandlerRegistration addCloseHandler(CloseHandler handler) { maybeInitializeCloseHandlers(); return addHandler(CloseEvent.getType(), handler); @@ -531,7 +535,6 @@ public static HandlerRegistration addCloseHandler(CloseHandler handler) * @return returns the handler registration */ public static HandlerRegistration addResizeHandler(ResizeHandler handler) { - maybeInitializeCloseHandlers(); maybeInitializeResizeHandlers(); return addHandler(ResizeEvent.getType(), handler); } @@ -556,7 +559,7 @@ public static void addWindowCloseListener(WindowCloseListener listener) { */ public static HandlerRegistration addWindowClosingHandler( ClosingHandler handler) { - maybeInitializeCloseHandlers(); + maybeInitializeBeforeCloseHandlers(); return addHandler(Window.ClosingEvent.getType(), handler); } @@ -579,7 +582,6 @@ public static void addWindowResizeListener(WindowResizeListener listener) { */ public static HandlerRegistration addWindowScrollHandler( Window.ScrollHandler handler) { - maybeInitializeCloseHandlers(); maybeInitializeScrollHandlers(); return addHandler(Window.ScrollEvent.getType(), handler); } @@ -910,13 +912,21 @@ private static WindowHandlers getHandlers() { return handlers; } + @Deprecated private static void maybeInitializeCloseHandlers() { if (GWT.isClient() && !closeHandlersInitialized) { - impl.initWindowCloseHandler(); + impl.initWindowUnloadHandler(); closeHandlersInitialized = true; } } + private static void maybeInitializeBeforeCloseHandlers() { + if (GWT.isClient() && !beforeCloseHandlersInitialized) { + impl.initWindowBeforeUnloadHandler(); + beforeCloseHandlersInitialized = true; + } + } + private static void maybeInitializeResizeHandlers() { if (GWT.isClient() && !resizeHandlersInitialized) { impl.initWindowResizeHandler(); diff --git a/user/src/com/google/gwt/user/client/impl/WindowImpl.java b/user/src/com/google/gwt/user/client/impl/WindowImpl.java index 4854d617b8..a1183f75d8 100644 --- a/user/src/com/google/gwt/user/client/impl/WindowImpl.java +++ b/user/src/com/google/gwt/user/client/impl/WindowImpl.java @@ -29,11 +29,30 @@ public native String getHash() /*-{ public native String getQueryString() /*-{ return $wnd.location.search; }-*/; - - public native void initWindowCloseHandler() /*-{ + + @Deprecated + public void initWindowCloseHandler() { + initWindowUnloadHandler(); + initWindowBeforeUnloadHandler(); + } + + @Deprecated + public native void initWindowUnloadHandler() /*-{ + var oldOnUnload = $wnd.onunload; + + $wnd.onunload = $entry(function(evt) { + try { + @com.google.gwt.user.client.Window::onClosed()(); + } finally { + oldOnUnload && oldOnUnload(evt); + $wnd.onunload = null; + } + }); + }-*/; + + public native void initWindowBeforeUnloadHandler() /*-{ var oldOnBeforeUnload = $wnd.onbeforeunload; - var oldOnUnload = $wnd.onunload; - + // Old mozilla doesn't like $entry's explicit return statement and // will always pop up a confirmation dialog. This is worked around by // just wrapping the call to onClosing(), which still has the semantics @@ -43,7 +62,9 @@ public native void initWindowCloseHandler() /*-{ try { ret = $entry(@com.google.gwt.user.client.Window::onClosing())(); } finally { - oldRet = oldOnBeforeUnload && oldOnBeforeUnload(evt); + try { + oldRet = oldOnBeforeUnload && oldOnBeforeUnload(evt); + } catch (ignored) {} } // Avoid returning null as IE6 will coerce it into a string. // Ensure that "" gets returned properly. @@ -55,18 +76,6 @@ public native void initWindowCloseHandler() /*-{ } // returns undefined. }; - - $wnd.onunload = $entry(function(evt) { - try { - @com.google.gwt.user.client.Window::onClosed()(); - } finally { - oldOnUnload && oldOnUnload(evt); - $wnd.onresize = null; - $wnd.onscroll = null; - $wnd.onbeforeunload = null; - $wnd.onunload = null; - } - }); }-*/; public native void initWindowResizeHandler() /*-{ diff --git a/user/src/com/google/gwt/user/client/ui/RootPanel.java b/user/src/com/google/gwt/user/client/ui/RootPanel.java index cc571e07c6..4d6cf11c3f 100644 --- a/user/src/com/google/gwt/user/client/ui/RootPanel.java +++ b/user/src/com/google/gwt/user/client/ui/RootPanel.java @@ -18,13 +18,10 @@ import com.google.gwt.dom.client.BodyElement; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; -import com.google.gwt.event.logical.shared.CloseEvent; -import com.google.gwt.event.logical.shared.CloseHandler; import com.google.gwt.i18n.client.BidiUtils; import com.google.gwt.i18n.client.HasDirection; import com.google.gwt.i18n.client.LocaleInfo; import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.Window; import java.util.HashMap; import java.util.HashSet; @@ -188,8 +185,6 @@ public static RootPanel get(String id) { // on the first RootPanel.get(String) or RootPanel.get() // call. if (rootPanels.size() == 0) { - hookWindowClosing(); - // If we're in a RTL locale, set the RTL directionality // on the entire document. if (LocaleInfo.getCurrentLocale().isRTL()) { @@ -258,15 +253,6 @@ private static native Element getRootElement() /*-{ return $doc; }-*/; - private static void hookWindowClosing() { - // Catch the window closing event. - Window.addCloseHandler(new CloseHandler() { - public void onClose(CloseEvent closeEvent) { - detachWidgets(); - } - }); - } - /* * Checks to see whether the given element has any parent element that * belongs to a widget. This is not terribly efficient, and is thus only used