Can I detect IE Document Mode on the server side using the HttpBrowserCapabilities object

青春壹個敷衍的年華 提交于 2019-12-04 04:59:28

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9430449/can-i-detect-ie-document-mode-on-the-server-side-using-the-httpbrowsercapabiliti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!