1

I have following code:

public static void show(String value){
   Window.alert("From Java");
   invokeMethod(); //Does not get called
}  

public static native void invokeMethod() /*-{ 
    alert("From JSNI");
}-*/; 

I want to call a JSNI method from Java method, however invokeMethod() never gets called... I could not find much use cases for calling JSNI method from Java.

Why isn't above code working?

3
  • your code should work. Something else going wrong outside this code maybe? Commented May 28, 2012 at 18:00
  • 1
    change the jsni to use $wnd.alert instead of alert. And make sure to click 'ok' on the first alert, or the second alert can't run. Commented May 28, 2012 at 18:17
  • "And make sure to click 'ok' on the first alert, or the second alert can't run." this doesn't matter since a alert, both alerts will get triggerd Commented May 29, 2012 at 9:55

1 Answer 1

1

First of all, as Colin Alworth said, you need to change your code to $wnd.alert("From JSNI"); If you want to call another JS function apart from alert(), you should write the body of your function in your html page. You'll find all information needed here

So your code should look like this:

public static void show(String value){
   Window.alert("From Java");
   invokeMethod(); 
}  

public static native void invokeMethod() /*-{ 
    $wnd.alert("From JSNI"); //Added "$wnd."
}-*/; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.