Access Javascript variable in GWT

I have a variable in Javascript that switches between true and false when full screen mode is turned on and off, respectively. Now I want to access this variable in my GWT code and perform the appropriate actions. Can someone tell me how to do this? I could not figure it out from the Google documentation on JSNI ...

+4
source share
2 answers

In javascript

 var mybool = true; 

your JSNI method in the MyClass class;

 public static native boolean getNativeVariableType(String jsVar)/*-{ return eval('$wnd.' + jsVar); }-*/; 

Finally, use in GWT;

 boolean getFormJs = Myclass.getNativeVariableType("mybool"); 

As @dodoot raised the point, you can try this return !!$wnd[jsVar] to get side effects of the ridoff eval function.

As @manolo said, if you use gwtQuery , it will be more convenient simply by writing $(window).prop("mybool") .

+6
source

As a curiosity, for simple things like this, you can avoid writing jsni methods by taking advantage of some jsni methods already present in the gwt overlay classes.

So, in your case, you can get the window object and pass it to the element, read its properties using 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 is much simpler:

 boolean mybool = $(window).prop("mybool"); 
+6
source

All Articles