views:

584

answers:

1

I am trying to write browser specific code. Is there a GWT API to find out which browser the client is using?

+6  A: 

The GWT Developer's Guide page on Cross-Browser Support gives a JSNI function that returns the UserAgent string.

Note, however, that you probably want to use Deferred Binding to write browser-specific code, instead of detecting the UserAgent.

Edit: Kasturi points out Window.Navigator.getUserAgent(), which is implemented like so:

/**
 * Gets the navigator.appName.
 *
 * @return the window's navigator.appName.
 */
public static native String getAppName() /*-{
  return $wnd.navigator.appName;
}-*/;

So yes, this should do what the function mentioned on the Cross-Browser Support page does (except that it doesn't call toLowerCase() on it), though again you may be better off using deferred binding.

aem
Just found out that Navigator.getUserAgent() can be used to detect. Will that be a good idea?
Kasturi
Deferred binding is generally a better choice because the GWT sends browser specific code to that browser (that is, code just for IE will only be sent to IE browsers) thus reducing download size.However, this limits you to the user agent strings that GWT recognizes. If, for instance, you want to display a message to IE6.0.1.001 users, and not to other IE users, you're better off with the using Navigator.getUserAgent.If you want to know what broad user agent categories GWT supports, check out this blog post I wrote: http://www.mooreds.com/wordpress/archives/000574
mooreds