Recently I was working on a project that required me to force the user into IE8 document mode, but ONLY if they were using IE9 compatibility view (CV). If you are unfamiliar with the method of forcing a particular document mode, you can read more about it in this MSDN article. It looks a little bit like this:
<head> <title>My Page</title> <meta http-equiv="X-UA-Compatible" content="IE=8" /> </head>
This meta tag is placed in the head section of your page and must be placed before all other elements (with the exception of the title element and other meta tags). This is a perfectly valid way to force IE8, however I needed to make sure this was only done for IE9 CV, and that brings me to the topic of this post.
Regardless of your reasons to detect CV in IE, here is how you can accomplish it…
Each version of IE can be told apart by its user agent string. I won’t get into the details of the UA string as its history is lengthy, but you can read all about it here. The main elements that we need to look at are the Version token and the Trident token. Below are the tokens that you will find in each browser:
| VERSION | TRIDENT | |
| IE9 Standard | MSIE 9.0 | Trident/5.0 |
| IE9 CV | MSIE 7.0 | Trident/5.0 |
| IE8 Standard | MSIE 8.0 | Trident/4.0 |
| IE8 CV | MSIE 7.0 | Trident/4.0 |
| IE7 | MSIE 7.0 | No Trident token |
| IE6 | MSIE 6.0 | No Trident token |
As you can see, each browser has a unique combination of these two tokens. We can use this knowledge to now create a function that will tell us what browser mode is in use. My function is shown below:
private string GetIEBrowserMode()
{
string mode = "";
string userAgent = Request.UserAgent; //entire UA string
string browser = Request.Browser.Type; //Browser name and Major Version #
if (userAgent.Contains("Trident/5.0")) //IE9 has this token
{
if (browser == "IE7")
{
mode = "IE9 Compatibility View";
}
else
{
mode = "IE9 Standard";
}
}
else if (userAgent.Contains("Trident/4.0")) //IE8 has this token
{
if (browser == "IE7")
{
mode = "IE8 Compatibility View";
}
else
{
mode = "IE8 Standard";
}
}
else if (!userAgent.Contains("Trident")) //Earlier versions of IE do not contain the Trident token
{
mode = browser;
}
return mode;
}
Pretty simple once you know what to look for. The last step for my requirements was to force the browser into IE8 document mode if the user was using IE9 CV.
protected void Page_Load(object sender, EventArgs e)
{
if (GetIEBrowserMode() == "IE9 Compatibility View")
{
HtmlMeta force = new HtmlMeta();
force.HttpEquiv = "X-UA-Compatible";
force.Content = "IE=8";
Header.Controls.AddAt(0, force);
}
}
This snippet is very straightforward. We are creating a new meta tag with the necessary attributes, and we are adding it to the header of our page at an index of 0 because it needs to be among the first header elements. Now run your page! If you’d like to test the results, a good way to do so is to open IE Developer Tools (F12) and at the top there will be a drop down titled “Browser Mode”. Add a label to your page that shows the result of GetIEBrowserMode() and you can easily switch between the different browser modes with an accurate detection.
Lastly, IE10 is on the horizon and will contain different tokens in its UA string as well. The IE10 Trident token is now Trident/6.0, and compatibility mode for IE10 will still map to IE7 standards. It might be a good idea to add this to your function as it will be in use soon. Below are a few very helpful links on UA strings (IE in particular).
- MSDN Library: Understanding User-Agent Strings
- IEBlog: Changes in UA string between IE versions – IE7, IE8, IE9, and IE10






















