IE11通过javascript检测兼容性视图是否为ON

IE11通过javascript检测兼容性视图是否为ON

本文介绍了IE11通过javascript检测兼容性视图是否为ON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在网站上通过javascript时,有谁知道如何检查IE 11兼容模式是否开启?

does anyone know how to check if IE 11 compatibility mode is ON when I'm on a website through javascript?

我将网址添加到列表兼容性视图设置中。但当我这样做时

I added the url to the list compatibility view settings. But when I do

navigator.userAgent

在开发人员工具中,它返回

in developer tools, it returns

查看微软网站(),它说

Looking at the microsoft website (http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx), it says

检测页面是否通过javascript使用兼容性视图的任何帮助都会非常有用。提前致谢。

Any help on detecting whether a page is using compatibility view via javascript would be really helpful. Thanks in advance.

推荐答案

已解决

在自己寻找这个问题的答案时,我在另一个帖子中找到了来自Nenad Bulatovic的解决方案,但他的回答没有被标记为正确答案。我在IE11中测试了它并降级到IE5,发现它适用于IE7-IE11,这很棒。我想在这里分享,以防其他人觉得它有用。

While searching for an answer to this question myself, I found this solution from Nenad Bulatovic in another thread but his response wasn't marked as the correct answer. I tested this out in IE11 and downgrading to IE5 and found that it works for IE7-IE11, which is great. I wanted to share it here in case anyone else finds it useful.

iecheck.js

function trueOrFalse() {
    return true;
}

function IeVersion() {
    //Set defaults
    var value = {
        IsIE: false,
        TrueVersion: 0,
        ActingVersion: 0,
        CompatibilityMode: false
    };

    //Try to find the Trident version number
    var trident = navigator.userAgent.match(/Trident\/(\d+)/);
    if (trident) {
        value.IsIE = true;
        //Convert from the Trident version number to the IE version number
        value.TrueVersion = parseInt(trident[1], 10) + 4;
    }

    //Try to find the MSIE number
    var msie = navigator.userAgent.match(/MSIE (\d+)/);
    if (msie) {
        value.IsIE = true;
        //Find the IE version number from the user agent string
        value.ActingVersion = parseInt(msie[1]);
    } else {
        //Must be IE 11 in "edge" mode
        value.ActingVersion = value.TrueVersion;
    }

    //If we have both a Trident and MSIE version number, see if they're different
    if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) {
        //In compatibility mode if the trident number doesn't match up with the MSIE number
        value.CompatibilityMode = value.TrueVersion != value.ActingVersion;
    }
    return value;
}

iecheck.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Testing IE Compatibility Mode</title>
    <script src="iecheck.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results: </div>
</br>
<script type="text/javascript">

var ie = IeVersion();

document.write("IsIE: " + ie.IsIE + "</br>");
document.write("TrueVersion: " + ie.TrueVersion + "</br>");
document.write("ActingVersion: " + ie.ActingVersion + "</br>");
document.write("CompatibilityMode: " + ie.CompatibilityMode + "</br>");

</script>
</body>
</html>

来源:

这篇关于IE11通过javascript检测兼容性视图是否为ON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:54