简体   繁体   中英

how to call JSNI function from Javascript function?

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.

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();'>

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)

See here :

  1. Make sure that Test_GoogleWeb_JSNI.Callee() is static .
  2. Assign the Callee()-function to the window object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM