Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 that method from Java GWT code(Button's click event)..?

Can anyone please tell me code or way for accessing js file's function.

Thanks in Advance.

share|improve this question

2 Answers

up vote 3 down vote accepted
  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!

share|improve this answer
this will not work with some linker since the global window object (in which you are looking up myJSfunction) might not be the same window object. (gwt can load code in an hidden iframe...) – Daniel Kurka Aug 16 '12 at 14:39
Thanks. Its working. – iManan Aug 17 '12 at 6:22

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();
}-*/;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.