I have a variable in Javascript, which toggles between true and false when full-screen mode is switched on and off, respectively. Now I want to access that variable in my GWT code, and do some actions accordingly. Can anyone tell me how to do it? I couldn't understand it from the Google documentation on JSNI...
-
The selected answer doesnt work !user1711270– user17112702013-03-18 13:02:48 +00:00Commented Mar 18, 2013 at 13:02
2 Answers
In JavaScript
var mybool = true;
your JSNI method in MyClass class ;
public static native boolean getNativeVariableType(String jsVar)/*-{
return eval('$wnd.' + jsVar);
}-*/;
Finally using in GWT ;
boolean getFormJs = Myclass.getNativeVariableType("mybool");
As @dodoot raised the point you can try this return !!$wnd[jsVar] to get ridoff eval function side effects.
As @manolo said if you are using gwtQuery it will be more handy by writing simply $(window).prop("mybool").
3 Comments
Just as a curiosity, for simple things like this, you can avoid writing jsni methods by taking advantage of certain jsni methods already present in gwt overlay classes.
So in your case you can get the window object, and casting it to an element, read its properties with getters from the Element class :
Element $wnd = (Element)Document.get().<Element>cast().getPropertyObject("defaultView");
boolean mybool = $wnd.getPropertyBoolean("mybool");
Adding the gwtquery library to your project, the code is much simpler:
boolean mybool = $(window).prop("mybool");