2

I can find out the browser version and specific capabilities using the HttpBrowserCapabilities object, but is there a way I can find the Document Mode that the browser is using to render?

The equivalent of the JavaScript property document.documentMode

The mode can be changed via the developer tools

Update

I was hoping to include a separate stylesheet for IE7 and below using something like this in my razor layout page...

@if (ViewContext.IsBrowserOlderThanIE8()) {
<link href="Ie7.css" rel="stylesheet" type="text/css" />
}

Within function IsBrowserOlderThanIE8 I can detect the browser version, but this is not enough to know what document mode the client is using. The document mode has more baring on the actual rendering engine used.

Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
  • Define "simply". AFAIK it´s feature detection and set cookie in the browser; read cookie on the server. – anddoutoi Feb 24 '12 at 12:29
  • In this case "Simply" has no comparative meaning. I just used it as a figure of speech. It shouldn't be there. Sorry about that. I'm not really bothered how hard or easy it is, as long as there is a way! Thanks for your suggestion. – Andy McCluggage Feb 24 '12 at 15:25
  • Are you not able to just pass your document.documentMode information to your server side? – Etch Feb 24 '12 at 15:49
  • 1
    How would you do this for the first request? – kralco626 Feb 24 '14 at 18:44

3 Answers3

3

You should be able to use the Request.UserAgent to get the string that has the capability flag in it.

Check these 2 links.

Understanding User-Agent strings

User-Agent Properties

Edit Figured I would add more detail. Basically the Trident token of the User-Agent string is the REAL version of the browser and the MSIE token is the browser mode it is using. You can easily check this out by using the first link and running fiddler to see what the HTTP headers look like.

MORE EDIT I turn on fiddler and browse to www.yahoo.com with IE9.0 and see User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) I then hit the compatibility mode button and see:

User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7)

See how the trident = 5.0 both times but the MSIE is 9.0 and then 7.0?

Depending on the URL you are going to there are ways to force this information. For instance on the web app I am working right now we force IE7 Compatibility mode because of some various reasons.

Etch
  • 3,044
  • 25
  • 31
  • On the server side if I look at the `UserAgent` property of the `HttpRequest`, I always get the following value `Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ENGB)` regardless of the document mode selected in the browser. Are you not seeing that? – Andy McCluggage Feb 24 '12 at 16:29
  • Yes I do get that User-Agent string when I use computability mode. That could be useful. But what I really need is the Document Mode. The mode set through the menu on the developer tools...http://msdn.microsoft.com/en-us/library/dd565626(v=vs.85).aspx#docModeMenu – Andy McCluggage Feb 24 '12 at 17:28
  • 1
    The document mode is the same thing as the compatibility mode. If Trident =5.0 means the browswer is 9.0 and the MSIE is 7.0 that means its running in document mode ie compatibility mode IE7. Unless I am totally misunderstanding the question. – Etch Feb 24 '12 at 18:30
1

No. you cannot determine the document mode via server side code. You can check the compat. mode with the trident value in the user agent, which defaults the document mode. Key word..DEFAULTS..IT CAN STILL BE CHANGED via F12. If a user then changes the document mode again, to something besides what compat. mode changes it to, then you will not be able to see the change.

0

You can determine it on the client side, then have the client request the correct css file...

<head>
<script type="text/javascript>"
    ...
    var choice;
    if (condition) {
        choice = 'ie7';
    } else {
        choice = 'default';
    }
    document.writeln('<link type="text/css" rel="stylesheet" href="' + choice + '.css" />');
</script>
...
</head>

And of course you could make that little "selector" script a file that's included, rather than actually coding it in-line in each page you make.

Andrew
  • 4,953
  • 15
  • 40
  • 58
  • That is a very good point. Why was I bothering to try and do this server side when client side would have worked just as well.I think I ended up solving this another way back then. – Andy McCluggage Nov 21 '14 at 13:30