0

I am trying to call my Javascript method from my java in GWT below is what i am doing

public void onModuleLoad() {
    jsniAlert("test");
}

private static final native void jsniAlert(String test) /*-{
    $wnd.alert(test);
    $wnd.testJavascript();
}-*/;

HelloJSNI.html (the main html class which use to open my application in war folder)

<script type="text/javascript" language="javascript"  
    src="hellojsni/hellojsni.nocache.js"></script>

<script type="text/javascript">
    function testJavascript(var input) {
        window.jsniAlert();
        var var1inJS = "Default value";

        alert("Value of Var1 = " + var1inJS);
        var1inJS = input;
        alert("Value of Var1 = " + var1inJS);

        var var2inJS = "Waht is the value of Var2";

        alert("Value of Var2 = " + var2inJS);   
    }

but when i run my application there's an exception

javascriptexception:object doesn't support property or method 'testjavascript'
Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
user1226162
  • 1,972
  • 8
  • 27
  • 42
  • This is the same question as http://stackoverflow.com/questions/10050909/how-can-i-access-some-javascript-file-from-java-in-gwt/ – Goran Nastov Apr 09 '12 at 10:58

2 Answers2

1

You have an error in your testJavascript function, so it is not getting loaded, and you can't call it.

Change this:

function testJavascript(var input) {

to this

function testJavascript(input) {  //notice that var keyword is not used to define parameters
jusio
  • 9,850
  • 1
  • 42
  • 57
  • changed it from var input to input , but getting same error, any suggestion , thanks – user1226162 Apr 09 '12 at 10:44
  • You are most likely getting a different error ` Object [object DOMWindow] has no method jsniAlert`. And it is normal because there is no javascript function jsniAlert in your code, and you are calling it from `testJavascript` function – jusio Apr 09 '12 at 10:58
0

your javascript function is not loaded. in the browsers, when a browser detacts an error in the code between script tags, the code inside will not be loaded and it could lead into unexpected results.

GingerHead
  • 8,130
  • 15
  • 59
  • 93
  • Man you have a big trouble here: you are calling a function B from a function A, then this function B calls back the function A, which will be a dead loop of calling functions! remove window.jsniAlert(); from your testJavascript – GingerHead Apr 09 '12 at 11:40
  • thanks , removed jsniAlert , in fact i remove everything from the method,it will do nothing but it shouldn't give me any exception atleast. – user1226162 Apr 09 '12 at 14:23