65.9K
CodeProject is changing. Read more.
Home

How your web site can determine if the user is running their IE browser in compatibility mode.

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.86/5 (5 votes)

Sep 19, 2014

CPOL

2 min read

viewsIcon

15262

This article show a quick way to determine if a user's IE browser is in compatibility mode.

Compatibility.png

Introduction

As more websites are using HTML5 and 3rd party controls that use HTML5, we do not want a user’s browser using compatibility mode.  This article suggests one way you can determine if a browser is in compatibility mode.

Background

The company I work for used to require IE users to put their browser in compatibility mode for our web site to render properly.  Then we moved to a newer version of .net and HTML5.  Now we want our users to no longer use compatibility mode.

The Problem

When users log into our website we want to provide a message that will let them know they have compatibility mode turned on in their IE browser.  The problem is: What is the easiest way to figure out if they have compatibility mode turned on?

The Solution

I spent a bit of time trying to find the best way to figure out if a browser is in compatibility mode.  When you google how to find a browser's compatibility mode you will find all sorts of solutions.  There is a JavaScript solution.  There is a solution where you look at the Request.UserAgent in your .net code.  As I looked through these options, they seemed more complicated and could possibly break as new versions of IE come out.  As I looked for a common thread I found that in any browser, as of this writing we are at IE11, when put into compatibility mode looks like IE7.  So here is my solution:

'VB.net 
'check for compatibility mode
If Request.Browser IsNot Nothing AndAlso _
   Request.Browser.Browser = "IE" AndAlso _
   Request.Browser.Version = "7.0" Then
   'show error or do action
End If

 

//C#
//check for compatibility mode
if (Request.Browser != null && Request.Browser.Browser == "IE" && 
    Request.Browser.Version == "7.0") {
    //show error or do action
}

Of course the downside to this solution is that if someone is actually using IE7 they will see a message that tells them to turn off compatibility mode.  This is a risk I am willing to take.

Conclusion

This is a simple solution to determine in your website if a user is using compatibility mode.  If you need something more complicated there are more solutions out there.  I found this simple solution to work well for me.

History

First release.