If
you decide to stop supporting all types of browsers, mainly older IE
browsers, you’ll probably be required to detect the browser and it’s
version using either client or server programming. Client browser detection can use viewed here.
This blog entry focuses on an ASP.NET server side solution,
which will allow not only to detect if the browser is supported, but on
how to detect IE8 running in Compatibility View (optionally notifying
the user on how to switch from this view to IE8).
The
solution is quite simple - it relies on the user-agent string sent by
the browser. Browsers send information to the server, one of them is
HTTP_USER_AGENT, which describes the user’s browser. For example, IE
browsers incorporate the keyword MSIE
plus the browser’s version. So IE6 will show as MSIE 6, IE7 will show
up as MSIE 7 and IE8 will show up as MSIE 8. But how will IE8 appear in
Compatibility View (that is, IE7)? Apparently, the browser will send a string with MSIE
7. So, how can we tell whether this is really an IE7, or IE8 in
Compatibility View? The answer relies also in the user-agent string: IE8
incorporates a new “Trident”
token in the user-agent string, even in Compatibility View. So by
combining a check for MSIE 7 with Trident, allows understanding that the
user is running in Compatibility View.
In short, the following ASP.NET code will detect if the browser is running in Compatibility View:
bool
isCompatibilityMode = (Request.UserAgent.IndexOf("MSIE 7") > -1
&& Request.UserAgent.IndexOf("Trident") > -1);
Naturally,
you may choose to refactor this code to “better looking code”, such as
testing for IE first, and then testing for the major version of the
browser etc’.
You
can also combine this code in different scenarios: You can detect other
browsers, you can block certain browsers, you can notify the users that
they’re using unsupported browsers, and you can notify how to switch off
Compatibility View etc’. It could also be a good practice to store the
user’s decision in a Cookie, Session or some other Profile settings, in
order not to process this code every time the browser makes a request.
You can read about the cause for this code in the first place in Browser Wars and Compatibility in Internet Explorer (Hebrew).
To see what your user agent is, click here.
Currently rated 2.4 by 7 people
- Currently 2.428571/5 Stars.
- 1
- 2
- 3
- 4
- 5