Search code examples
javajavascriptgwtjsni

Passing variable name into GWT's JSNI


Using the JavaScript Native Interface of GWT I can perform the following:

public native static String getNativeVariableFoo() /*-{
    return $wnd.foo;
}-*/;

Which will return the contents of a JavaScript variable called foo.

How can I expand upon this to accept the variable name as a parameter? ie:

public native static String getNativeVariable(String foo) /*-{
    /* Somehow meaningfully concat '$wnd.' with value of foo */
}-*/;

Simply using the variable name inside the native code like one would to call:

eval(foo)

results in the JavaScript hunting for a declaration of a variable named foo and not one named with the value of foo.

Thanks very much!


Solution

  • Does

    $wnd[foo] 
    

    not work?

    You may also want to look at the GWT 'Dictionary' class. It's ideal for loading values, i.e. parameters from the host page.