views:

1080

answers:

1

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!

+1  A: 

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.

AlexJReid
Brilliant!I'll look into the Dictionary too although we're aiming for re-usable JSP tags which can grab particular session attributes and dump them to pages where they're needed. It's not perfect but the Dictionary still requires two things to think about and the tags give us easy validation against a TLD. Thanks!
Chris J