27

Is there GWT api that will tell me which browser version it detected?

I've found a flaw with IE7's regex handling and need to code around some tricky String.matches() expressions.

Stevko
  • 4,345
  • 6
  • 39
  • 66
  • 1
    Could you describe the IE7 flaw for us? What is a sample regex and in what way IE7 gets it wrong (and other browsers get it right)? I don't think it affects the question or answers, just would be nice to have that information here for posterity. – Stephen P Jun 17 '10 at 20:42
  • I've got a password regex that matches==true in every browser tested besides IE7. PASSWORD_REGEX = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,24})" – Stevko Jun 18 '10 at 06:24

3 Answers3

28

You can detect the browser type using the code below.

public static native String getUserAgent() /*-{
return navigator.userAgent.toLowerCase();
}-*/;

Then you can call that function and look at the type of the browser. For example the code below decides whether it is internet explorer or not.

if(getUserAgent().contains("msie"))
{
///////// Write your code for ie
}

This page has the User Agent for just about every browser known to man.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
  • 29
    How about [`Window.Navigator.getUserAgent()`](http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/Window.Navigator.html#getUserAgent%28%29) instead of your JSNI method? – Thomas Broyer May 09 '11 at 09:01
  • How would i know whether the browser is 'Mozilla Fire Fox/Safari/Opera/Chrome' or not. can you post the same condition for the other browsers as you have done for IE. – Jagadeesh Aug 07 '12 at 09:33
  • 5
    Also, this will return **false** for **IE11**, since it's userAgent string does not contain `msie`. One should use `contains("msie") || contains("trident")` to match **IE11**. – Yuriy Nakonechnyy Aug 04 '14 at 09:38
27

You could use GWT deferred binding using replacement and create two implementations of your class in which you use regex.

For example let's assume your class is named Parser and it contains code for all web browsers except for IE7. Then you can extend Parser and create ParserIE7 class for IE7. Then in your GWT module config file you can add:

<replace-with class="Parser">
  <when-type-is class="Parser"/>
</replace-with>

<replace-with class="ParserIE7">
  <when-type-is class="Parser" />
  <when-property-is name="user.agent" value="ie7"/>
</replace-with>

Then by calling

Parser parser = GWT.create(Parser.class);

you should have a proper (web browser dependent) implementation of Parser in parser variable.

You can find more details here.

Piotr
  • 5,543
  • 1
  • 31
  • 37
  • +1 This is a valid answer if you have a lot of changes between different parsers and want to minimize traffic between the server and the browser. – Romain Hippeau Jun 17 '10 at 19:25
7

If you are using the GXT library, you can use GXT.isChrome to detect chrome and you can find different data members of GXT class to detect a particular browser.

Gnoupi
  • 4,715
  • 5
  • 34
  • 50
  • 3
    Using 3rd party libraries for such basic/trivial tasks (regardless of popular they might be for other purposes) is very bad habit. – Jaroslav Záruba Oct 10 '11 at 08:47
  • 4
    Downvoting is a bit harsh still though. And GXT is a very popular library, people finding this post may already be using it. – Thomas Ahle Jul 17 '12 at 11:41