简体   繁体   中英

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!

Does

$wnd[foo] 

not work?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM