如何检测IE的版本

时间:2013-05-07 06:48:24

标签: javascript internet-explorer browser-detection

我使用以下功能来检测我的IE版本:

function isIE() 
{
    var myNav = navigator.userAgent.toLowerCase();
    return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}

如果使用IE浏览器,此代码将返回IE版本,如果使用其他浏览器,则返回false。它可以在IE6,IE9和Chrome中使用。当我尝试Firefox最新版本v20时,它不起作用(网站挂起)。你们知道为什么吗?

或者是否有其他功能可用于检测IE版本?

8 个答案:

答案 0 :(得分:1)

JQuery有$ .browser:http://api.jquery.com/jQuery.browser/

你可以这样做:

if ($.browser.msie){
}

答案 1 :(得分:0)

我用这个

public static class DeviceHelper
{

    public static bool IsMobile(string userAgent)
    {
        userAgent = userAgent.ToLower();
        var currentSubContent = NodeLocator.GetNodeOfExactType<Language>();

        var isMobileDevicesActivatedInBackend = currentSubContent.ActiveForMobileDevices;
        if (isMobileDevicesActivatedInBackend)
        {
            return userAgent.Contains("iphone") |
             userAgent.Contains("ppc") |
             userAgent.Contains("windows ce") |
             userAgent.Contains("blackberry") |
             userAgent.Contains("opera mini") |
                //userAgent.Contains("mobile") |
             userAgent.Contains("palm") |
             userAgent.Contains("portable") |
             (userAgent.Contains("android") && userAgent.Contains("mobile")) |
             (userAgent.Contains("windows") && userAgent.Contains("mobile"));

        }
        else
        {
            return false;
        }
    } 
}

然后你可以从这里添加你需要的东西:检测MSIE的版本

HERE

或在jquery中使用他们的api:

if ($.browser.msie){
}

答案 2 :(得分:0)

使用此功能。

function checkVersion() {
        var msg = "You're not using Internet Explorer.";
        var ver = getInternetExplorerVersion();

        if (ver > -1) {
            if (ver >= 8.0)
                msg = "You're using a recent copy of Internet Explorer."
            else
                msg = "You should upgrade your copy of Internet Explorer.";
        }
        alert(msg+" version ->"+ver);
    }

答案 3 :(得分:0)

使用IE条件评论,这非常难看但很简单。

编辑:这是基于wikipedia's example

的更好版本
<script>
    var internet_explorer_version = -1;

/*@cc_on

  @if (@_jscript_version == 10)
    internet_explorer_version = 10;

  @elif (@_jscript_version == 9)
    internet_explorer_version = 9;

  @elif (@_jscript_version == 5.8)
    internet_explorer_version = 8;

  @elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
    internet_explorer_version = 7;

  @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
    internet_explorer_version = 6;

  @elif (@_jscript_version == 5.5)
    internet_explorer_version = 5.5;

  @end

@*/
</script>

IE4 +仅支持条件注释。较旧的IE或非IE浏览器将跳过所有检查并留下-1。这个脚本检查5.5+,这应该是你需要的任何东西。

答案 4 :(得分:0)

/**
 * Detects browser endgine and browser version.
 * @class BrowserDetect
 * @property {string} browser  browser engine
 * @property {number} version browser version 
 * @return {object}
 */
var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
                || this.searchVersion(navigator.appVersion)
                || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) !== -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index === -1)
            return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
        {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
        },
        {string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
        },
        {
            prop: window.opera,
            identity: "Opera",
            versionSearch: "Version"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {// for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {// for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS: [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
            string: navigator.userAgent,
            subString: "iPhone",
            identity: "iPhone/iPod"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]
};

答案 5 :(得分:0)

你可以使用正则表达式..

<script type="text/javascript">
var userAgent = navigator.userAgent;

var internet_version = -1; 
userAgent.replace(/MSIE .{1,5};/g, function(findS) {
    internet_version = parseFloat(findS.substring(5, findS.length-1));
});

alert(internet_version);
</script>

答案 6 :(得分:0)

这对我有用,我已经通过IE11在IE9上测试了它。 IE11中的useragent字符串与之前的版本不同,这就是为什么这里有两个不同的测试。

if (/msie|trident/ig.test(navigator.userAgent)) {
  var matches = navigator.userAgent.match(/MSIE\s([\d\.]+)/) || navigator.userAgent.match(/rv:([\d\.]+)/);
  var ieVersion = parseInt(matches[1], 10);
}

答案 7 :(得分:0)

从msdn获得此脚本:

    function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    // http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
    {
      var rv = -1; // Return value assumes failure.
      if (navigator.appName == 'Microsoft Internet Explorer')
      {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
      }
      return rv;
    }

就个人而言,我总是只使用条件注释将类应用于<html>。稍后,我在javascript中检查classname。