title: How to run JavaScript function from GWT Java with JSNI?
body: <p>Can't understand from the manual: how actually to run JS function from Java?</p>
  
  <p>For example, I have a function in my html page:</p>
  
  <pre><code>&lt;script type="text/javascript" language="javascript"&gt;
      function foo() {
          alert('Foo!');
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>The following module shows two buttons, only second of which works:</p>
  
  <pre><code>public class Test_GoogleWeb_JSNI_01 implements EntryPoint {
  
  public void onModuleLoad() {
  
      Button fooButton = new Button("Foo!");
      fooButton.addClickHandler(new ClickHandler(){
          public void onClick(ClickEvent event) {
              fooRunner();
          };
      });
  
  
      HTML fooButtonNative = new HTML();
      fooButtonNative.setHTML("&lt;input type='button' value='Foo Native' onclick='foo()'&gt;");
  
      RootPanel rootPanel = RootPanel.get();
      rootPanel.add(fooButton);
      rootPanel.add(fooButtonNative);
  
  }
  
  public static native void fooRunner() /*-{
    foo();
  }-*/;
  }
  </code></pre>
  
  <p>It is said in manual, that native functions implemented within nested frame, which explains the situation. But how to run JS functions then?</p>
  
  <p><strong>UPDATE 1</strong>
  The following works.</p>
  
  <p>Java:</p>
  
  <pre><code>public static native void fooRunner() /*-{
    $doc.fooRunner();
  }-*/;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>&lt;script type="text/javascript" language="javascript"&gt;
      document.fooRunner = function foo() {
          alert('Foo!');
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Is there a better way?</p>