1

Hi I am trying to call a function in a .js file from java.

In my Java code I have

@Override
public native void test()
/*-{
    //JAVASCRIPT
    $wnd.h8();
}-*/;

Just an Alert inside the method works but I can't call one of my functions. I've tryed $wnd, $doc and nothing.

Say I have a functions.js with this in it:

function h8(){
    alert("hi");
}

In my index I have:

<!doctype html>
<html>
       <head>

              <title>GWT Test</title>
              <meta http-equiv="content-type" content="text/html; charset=UTF-8">
              <link href="styles.css" rel="stylesheet" type="text/css">
              <script src="js/functions.js"></script> 

       </head>

       <body>
              <div align="center" id="embed-html"></div>
              <script type="text/javascript" src="html/html.nocache.js"></script>
       </body>

</html>

So there you see I am loading the script file in the html, but I get the 'undefined is not a function' error. Whithout $doc or $wnd I get the Can't find variable h8 error.

This is the console error:

GwtApplication: exception: (TypeError) 
 line: 112978
 column: 10
 sourceURL: http://127.0.0.1:9876/html/52574FB8FF8725CA72DFF813B62FEE86.cache.js
 __gwt$exception: <skipped>: undefined is not a function (evaluating '$wnd.h8()')
(TypeError) 
 line: 112978
 column: 10
 sourceURL: http://127.0.0.1:9876/html/52574FB8FF8725CA72DFF813B62FEE86.cache.js
 __gwt$exception: <skipped>: undefined is not a function (evaluating '$wnd.h8()')
3
  • 1
    What happens if you call h8() in JS console?, I suppose you are doing, but can you check if you are calling the test() method in your entry point? Commented Dec 2, 2014 at 6:43
  • The test() is called from java because if I change $wnd.h8() for a simple alert("hey") it does the alert. Commented Dec 2, 2014 at 16:59
  • 1
    You didn't answer my first question does h8 works in dev console. I guess you are defining in other scope. Commented Dec 2, 2014 at 17:41

1 Answer 1

3

You should define Window function:

Window.h8 = function() {
    alert("!!!");
};

And access it with $wnd.

private static native void callFunction() /*-{
    $wnd.h8();
}-*/;
Sign up to request clarification or add additional context in comments.

2 Comments

I call $wnd.h8(); from the Java test method and have Window.h8 = function(){ alert("hi"); }; in my js file and still undefined is not a function... [gwt$exception: <skipped>: undefined is not a function (evaluating '$wnd.h8()']
It was window.h8 with lower case. Thanks you both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.