Calling Javascript function of .js file from java GWT code

后端 未结 2 1872
忘掉有多难
忘掉有多难 2021-01-01 21:19

I have one button in Java GWT code. And I have one javascript file in scripts folder. I want to access functions of that js file on Button click.

So how can I call t

相关标签:
2条回答
  • 2021-01-01 21:47
    1. Import the JS Lib in your .html file.
    2. Create a method like this:

      public static native void onMyButtonClick() /*-{
          myJSfunction();
      }-*/;
      
    3. Bind your button like this:

      myButton.addClickHandler(new ClickHandler() {
      
          @Override
          public void onClick(ClickEvent event)
          {
              onMyButtonClick();
          } 
      
      });
      
    4. Done!

    Just make sure that the javascript containing your function is loaded before the generated GWT javascript.

    Your welcome!

    0 讨论(0)
  • 2021-01-01 22:00

    since your code should not depend on the gwt linker (and how it loads code) you need to prefix the call with the right window object. Reapp does not take that into account. So it actually needs to be:

    public static native void onMyButtonClick() /*-{
        $wnd.myJSfunction();
    }-*/;
    
    0 讨论(0)
提交回复
热议问题