0

我使用以下函数来检测我的 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版本?

4

8 回答 8

1

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

你可以做 :

if ($.browser.msie){
}
于 2013-05-07T06:54:46.937 回答
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"
        }
    ]
};
于 2013-05-07T07:21:28.257 回答
0

这很丑陋但很简单,使用 IE 条件注释。

编辑:这是基于维基百科示例的更好版本

<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+,对于您需要的任何东西,它应该足够远了。

于 2013-05-07T07:18:46.470 回答
0

这对我有用,我已经在 IE9 到 IE11 上对其进行了测试。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);
}
于 2014-07-14T13:46:28.803 回答
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>
于 2013-05-07T07:31:49.210 回答
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 的版本

这里

或者在 jquery 中只使用他们的 api:

if ($.browser.msie){
}
于 2013-05-07T06:55:25.417 回答
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);
    }
于 2013-05-07T07:00:12.700 回答
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 中检查类名。

于 2014-08-08T21:21:18.017 回答