Fixes a bug in the way Window sinks events on the outer Window.  In some cases, adding a script element to the page will not result in the script element being parsed synchronously, so we need to use the eval command to ensure the events are sunk on the outer Window.  IE does not work with the eval command, so we use a deferred binding.

Patch by: jlabanca
Review by: jgw



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@3606 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/Window.java b/user/src/com/google/gwt/user/client/Window.java
index d22e2ff..4e3b2a6 100644
--- a/user/src/com/google/gwt/user/client/Window.java
+++ b/user/src/com/google/gwt/user/client/Window.java
@@ -17,11 +17,8 @@
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
-import com.google.gwt.dom.client.Document;
-import com.google.gwt.dom.client.ScriptElement;
 import com.google.gwt.http.client.URL;
 import com.google.gwt.user.client.impl.WindowImpl;
-import com.google.gwt.user.client.ui.RootPanel;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -673,7 +670,7 @@
   }-*/;
 
   /**
-   * Embed a script on the outer window and use it to initialize an event.
+   * Initialize an event on the outer window.
    * 
    * @param initFunc the string representation of the init function
    * @param funcName the name to assign to the init function
@@ -681,19 +678,10 @@
    */
   private static void initHandler(String initFunc, String funcName, Command cmd) {
     if (GWT.isClient()) {
-      // Always intialize the close handlers first
+      // Always initialize the close handlers first
       maybeInitializeCloseHandlers();
 
-      // Embed the init script on the page
-      initFunc = initFunc.replaceFirst("function", "function " + funcName);
-      ScriptElement scriptElem = Document.get().createScriptElement(initFunc);
-      Document.get().getBody().appendChild(scriptElem);
-
-      // Initialize the handler
-      cmd.execute();
-
-      // Remove the init script from the page
-      RootPanel.getBodyElement().removeChild(scriptElem);
+      impl.initHandler(initFunc, funcName, cmd);
     }
   }
 
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 d5d9fa6..3eaeca2 100644
--- a/user/src/com/google/gwt/user/client/impl/WindowImpl.java
+++ b/user/src/com/google/gwt/user/client/impl/WindowImpl.java
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.user.client.impl;
 
+import com.google.gwt.user.client.Command;
+
 /**
  * Native implementation associated with
  * {@link com.google.gwt.user.client.Window}.
@@ -48,4 +50,17 @@
   public native int getScrollTop() /*-{
    return @com.google.gwt.user.client.impl.DocumentRootImpl::documentRoot.scrollTop;
   }-*/;
+
+  public void initHandler(String initFunc, String funcName, Command cmd) {
+    // Eval the init script
+    initFunc = initFunc.replaceFirst("function", "function " + funcName);
+    eval(initFunc);
+
+    // Initialize the handler
+    cmd.execute();
+  }
+
+  private native void eval(String expr) /*-{
+    $wnd.eval(expr);
+  }-*/;
 }
diff --git a/user/src/com/google/gwt/user/client/impl/WindowImplIE.java b/user/src/com/google/gwt/user/client/impl/WindowImplIE.java
index 9f66ac0..6e2d70c 100644
--- a/user/src/com/google/gwt/user/client/impl/WindowImplIE.java
+++ b/user/src/com/google/gwt/user/client/impl/WindowImplIE.java
@@ -15,6 +15,10 @@
  */
 package com.google.gwt.user.client.impl;
 
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.ScriptElement;
+import com.google.gwt.user.client.Command;
+
 /**
  * IE implementation of {@link com.google.gwt.user.client.impl.WindowImpl}.
  */
@@ -46,4 +50,23 @@
     var questionLoc = href.lastIndexOf("?");
     return (questionLoc > 0) ? href.substring(questionLoc) : "";
   }-*/;
+
+  /**
+   * IE6 does not allow direct access to event handlers on the parent window,
+   * so we must embed a script in the parent window that will set the event
+   * handlers in the correct context.
+   */
+  @Override
+  public void initHandler(String initFunc, String funcName, Command cmd) {
+    // Embed the init script on the page
+    initFunc = initFunc.replaceFirst("function", "function " + funcName);
+    ScriptElement scriptElem = Document.get().createScriptElement(initFunc);
+    Document.get().getBody().appendChild(scriptElem);
+
+    // Initialize the handler
+    cmd.execute();
+
+    // Remove the script element
+    Document.get().getBody().removeChild(scriptElem);
+  }
 }