How to find out about the User Agent in GWT

19,009

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.

Share:
19,009
Kasturi
Author by

Kasturi

Software Engineer Amazon

Updated on June 15, 2022

Comments

  • Kasturi
    Kasturi about 2 years

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

  • Kasturi
    Kasturi about 14 years
    Just found out that Navigator.getUserAgent() can be used to detect. Will that be a good idea?
  • mooreds
    mooreds about 14 years
    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: mooreds.com/wordpress/archives/000574