Browser version detect using GWT?
Asked Answered
A

3

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.

Akeyla answered 17/6, 2010 at 18:16 Comment(2)
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.Obduce
I've got a password regex that matches==true in every browser tested besides IE7. PASSWORD_REGEX = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,24})"Akeyla
M
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.

Maltzman answered 17/6, 2010 at 18:48 Comment(1)
+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.Exam
E
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.

Exam answered 17/6, 2010 at 18:40 Comment(3)
How about Window.Navigator.getUserAgent() instead of your JSNI method?Midway
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.Wellordered
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.Splay
M
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.

Maltzman answered 17/6, 2010 at 18:48 Comment(1)
+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.Exam
L
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.

Leshalesher answered 4/2, 2011 at 7:16 Comment(2)
Using 3rd party libraries for such basic/trivial tasks (regardless of popular they might be for other purposes) is very bad habit.Landloper
Downvoting is a bit harsh still though. And GXT is a very popular library, people finding this post may already be using it.Succinctorium

© 2022 - 2024 — McMap. All rights reserved.