5

Here is caller button samples in html:

<input type='button' value='Call' onclick='Test()'>

And here are some functions I tried and which were not worked:

<script type="text/javascript">
    function Test() {
        com.tests.client.Test_GoogleWeb_JSNI::Callee()();
    }
</script>

But we are not able to call Callee().How we can achieve this?I mean how we can invoke JSNI function from javascript?

Help would be appreciated.

  • possible duplicate of [How to call GWT java function from Javascript?](http://stackoverflow.com/questions/8619245/how-to-call-gwt-java-function-from-javascript) – sarnold Feb 18 '12 at 02:10

3 Answers3

8

It's very easy. You need to "export" your function written in GWT (or it can be another JSNI) function.

Here is the relevant documentation: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#calling

So in your case:

In your GWT code:

public static void Caller() /*-{ 
   ... 
}-*/

public static native void exportStaticMethod() /*-{
   $wnd.Callee =
      $entry(@com.tests.client.Test_GoogleWeb_JSNI::Callee());
}-*/;

Then you call exportStaticMethod() somewhere, even in your onModuleLoad. << YOU MUST DO THIS

Then you can call Callee() from your handwritten javascript code.

Your code for the button:

<input type='button' value='Call' onclick='$wnd.Callee();'>
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • Yes i tried but i am not able to invoke the function when we click on the button. –  Feb 14 '12 at 13:27
  • Have a look I updated my answer to show what the code for your button should be. Also if the function is not being called, I recommend you bring up your browser console (F12 in Chrome) and have a look for any exceptions. – Strelok Feb 14 '12 at 13:35
1

For chrome, the above solution works if I change onclick='$wnd.Callee() to onclick='window.Callee(). Chrome's browser console tells us $wnd is not defined. $wnd is how to access the browser's window object in JSNI.

Sorry I couldn't just leave this as a comment (not enough points)

timmacp
  • 191
  • 1
  • 13
0

See here:

  1. Make sure that Test_GoogleWeb_JSNI.Callee() is static.
  2. Assign the Callee()-function to the window object.
Bob
  • 5,510
  • 9
  • 48
  • 80