You can also test to see if your $wnd.variable is undefined before trying to use it and if it is then use a timer or schedule deferred or repeating to wait until it is available.
package com.testbrowser.client;
import com.google.gwt.core.client.JavaScriptObject;
public class BrowserVersionList extends JavaScriptObject {
protected BrowserVersionList() {
}
public static BrowserVersionList newInstance() {
return createJso();
}
private static native BrowserVersionList createJso() /*-{
return $wnd.brVersionMap;
}-*/;
public final native String getChromeVersion() /*-{
return this.Chrome;
}-*/;
}
The version js file:
brVersionMap={"IE":"8","Firefox":"15","Chrome":"17","Safari":"5"};
The main onModuleLoad body to show the version found:
public class BrowserSupportVersion implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
BrowserVersionList browserVersions = BrowserVersionList.newInstance();
GWT.log("Chrome version supported: "+browserVersions.getChromeVersion());
// Create the popup dialog box
final DialogBox dialogBox = new DialogBox();
dialogBox.setText("Detecting supported browser versions");
dialogBox.setAnimationEnabled(true);
// We can set the id of a widget by accessing its Element
final Label textToServerLabel = new Label();
VerticalPanel dialogVPanel = new VerticalPanel();
dialogVPanel.addStyleName("dialogVPanel");
dialogVPanel.add(new HTML("<b>Supported Chrome version: <i>"+browserVersions.getChromeVersion()+"</i></b>"));
dialogVPanel.add(textToServerLabel);
dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
dialogBox.setWidget(dialogVPanel);
RootPanel.get("notificationContainer").add(dialogBox);
}
}
I put this up and a version of my AppSpot account and it works from IE8, FF12, and Chrome 18.
http://4.runpartner.appspot.com/
Let me know if it works for you. Not sure what's causing your error. ( I cleaned the code up a little, but it ran as you had it too.)
Sincerely,
Joseph
package com.testbrowser.client;
import java.util.HashMap;
import java.util.Map;
public class BrowserVersionList {
Map<String,Integer> versionMap = new HashMap<String,Integer>();
public BrowserVersionList() {
loadValues(this);
}
/**
* Fetch the minimum supported browser version
* @param browserName
* @return NULL if no match found
*/
public int getVersion(String browserName) {
return versionMap.get(browserName);
}
public void putVersion(String key, int value) {
versionMap.put(key, Integer.valueOf(value));
}
/**
* Fetch values from JS object in browser.
* @param instance
*/
public final native void loadValues(BrowserVersionList instance) /*-{
for( var key in $wnd.brVersionMap) {
var value = $wnd.parseInt($wnd.brVersionMap[key],10);
instance.@com.testbrowser.client.BrowserVersionList::putVersion(Ljava/lang/String;I)(key,value);
}
}-*/;
}
package com.testbrowser.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class BrowserSupportVersion implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
BrowserVersionList browserVersions = new BrowserVersionList();
int chromeVersion = browserVersions.getVersion("Chrome");
GWT.log("Chrome version supported: "+chromeVersion);
// Create the popup dialog box
... same as before
}
}
Dictionary supportedVersions = Dictionary.getDictionary("brVersionMap");
int chromeVersion = Integer.valueOf(supportedVersions.get("Chrome"));