Search code examples
javahtmlgwtfullscreenjsni

Accessing Javascript variable in GWT


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...


Solution

  • 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").