最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

gwt - how to call JSNI function from Javascript function? - Stack Overflow

programmeradmin1浏览0评论

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() {
        .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.

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() {
        .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.

Share Improve this question asked Feb 14, 2012 at 12:40 user903539user903539 1
  • possible duplicate of How to call GWT java function from Javascript? – sarnold Commented Feb 18, 2012 at 2:10
Add a ment  | 

3 Answers 3

Reset to default 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./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(@.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 ment (not enough points)

See here:

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

评论列表(0)

  1. 暂无评论