Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中检测IE版本(v9之前)_Javascript_Internet Explorer_User Agent_Browser Detection - Fatal编程技术网

在JavaScript中检测IE版本(v9之前)

在JavaScript中检测IE版本(v9之前),javascript,internet-explorer,user-agent,browser-detection,Javascript,Internet Explorer,User Agent,Browser Detection,如果我们网站的用户使用的是v9之前版本的InternetExplorer,我想让他们跳转到错误页面。支持IE pre-v9不值得我们花时间和金钱。所有其他非IE浏览器的用户都很好,不应该被拒绝。以下是提议的代码: if(navigator.appName.indexOf("Internet Explorer")!=-1){ //yeah, he's using IE var badBrowser=( navigator.appVersion.indexOf("MS

如果我们网站的用户使用的是v9之前版本的
InternetExplorer
,我想让他们跳转到错误页面。支持
IE pre-v9
不值得我们花时间和金钱。所有其他非IE浏览器的用户都很好,不应该被拒绝。以下是提议的代码:

if(navigator.appName.indexOf("Internet Explorer")!=-1){     //yeah, he's using IE
    var badBrowser=(
        navigator.appVersion.indexOf("MSIE 9")==-1 &&   //v9 is ok
        navigator.appVersion.indexOf("MSIE 1")==-1  //v10, 11, 12, etc. is fine too
    );

    if(badBrowser){
        // navigate to error page
    }
}
这段代码会起作用吗

为了避免一些可能会出现在我面前的评论:

  • 是的,我知道用户可以伪造他们的
    useragent
    字符串。我不担心
  • 是的,我知道编程专家更喜欢嗅探功能支持而不是浏览器类型,但我觉得这种方法在这种情况下没有意义。我已经知道所有(相关的)非IE浏览器都支持我需要的功能,而所有
    v9之前的IE浏览器都不支持。在整个站点中逐个检查功能将是一种浪费
  • 是的,我知道有人试图使用
    IE v1
    (或>=20)访问该网站时,不会将“badBrowser”设置为true,并且警告页面也不会正确显示。这是我们愿意冒的风险
  • 是的,我知道Microsoft有“条件注释”,可以用于精确的浏览器版本检测。IE从
    IE10
    起不再支持条件注释,这使得这种方法毫无用处

  • 需要注意的任何其他明显问题?

    您的代码可以进行检查,但正如您所想,如果有人试图使用IE v1或>v19访问您的页面,则不会出现错误,因此可能更安全地使用正则表达式进行检查,如下代码所示:

    var userAgent = navigator.userAgent.toLowerCase();
    // Test if the browser is IE and check the version number is lower than 9
    if (/msie/.test(userAgent) && 
        parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]) < 9) {
      // Navigate to error page
    }
    
    var userAgent=navigator.userAgent.toLowerCase();
    //测试浏览器是否为IE,并检查版本号是否低于9
    如果(/msie/.test(userAgent)和
    parseFloat((userAgent.match(/.*(:rv | ie)[\/:](.+?)([\;].$)/)[1])<9){
    //导航到错误页面
    }
    
    如果没有其他人添加了
    addEventLister
    -方法,并且您使用的是正确的浏览器模式,那么您可以使用

    if (window.attachEvent && !window.addEventListener) {
        // "bad" IE
    }
    

    这是我最喜欢的方法。它提供了最大限度的控制。(注:条件语句仅在IE5-9中受支持。)

    首先正确设置ie类

    <!DOCTYPE html>
    <!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    <!--[if IE 7]>    <html class="lt-ie9 lt-ie8"> <![endif]-->
    <!--[if IE 8]>    <html class="lt-ie9"> <![endif]-->
    <!--[if gt IE 8]><!--> <html> <!--<![endif]-->    
    <head>
    

    感谢。

    使用条件注释。你正在尝试检测IE<9的用户,条件评论将在这些浏览器中工作;在其他浏览器(IE>=10和非IE)中,注释将被视为正常的HTML注释,这就是它们

    HTML示例:

    <!--[if lt IE 9]>
    WE DON'T LIKE YOUR BROWSER
    <![endif]-->
    
    
    
    如果需要,也可以使用脚本来完成此操作:

    var div = document.createElement("div");
    div.innerHTML = "<!--[if lt IE 9]><i></i><![endif]-->";
    var isIeLessThan9 = (div.getElementsByTagName("i").length == 1);
    if (isIeLessThan9) {
        alert("WE DON'T LIKE YOUR BROWSER");
    }
    
    var div=document.createElement(“div”);
    div.innerHTML=“”;
    变量IsElessThan9=(div.getElementsByTagName(“i”)。长度==1);
    如果(i小于9){
    提醒(“我们不喜欢你的浏览器”);
    }
    
    这对我很有用。我使用它作为一个页面的重定向,解释为什么我们不喜欢
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;URL=http://google.com">
    <![endif]-->
    

    返回IE版本,否则IE返回false

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

    if (isIE () == 8) {
     // IE8 code
    } else {
     // Other versions IE or not IE
    }
    

    //----------------------------------------------------------
    //用于检测JavaScript中IE版本的简短代码段
    //无需借助用户代理嗅探
    // ----------------------------------------------------------
    //如果您不在IE中(或IE版本小于5),则:
    //ie==未定义
    //如果您在IE(>=5)中,则可以确定哪个版本:
    //ie===7;//IE7
    //因此,要检测IE:
    //如果(即){}
    //以及检测版本:
    //ie===6//IE6
    //ie>7//IE8,IE9。。。
    //ie<9//任何低于IE9的东西
    // ----------------------------------------------------------
    //更新:现在使用@jdalton的实时节点列表想法
    变量ie=(函数(){
    var undef,
    v=3,
    div=document.createElement('div'),
    all=div.getElementsByTagName('i');
    当(
    div.innerHTML=“”,
    全部[0]
    );
    返回v>4?v:未定义;
    }());
    
    要轻松检测MSIE(v6-v7-v8-v9-v10-v11),请执行以下操作:

    if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
       // MSIE
    }
    

    我发现检查IE版本最全面的JS脚本是。整个图书馆都在

    在IE10中,不再支持条件语句

    在IE11中,用户代理不再包含MSIE。此外,使用用户代理是不可靠的,因为这是可以修改的

    使用PluginDetect JS脚本,您可以通过使用针对特定IE版本的非常特定且精心编制的代码来检测IE并检测确切的版本。当您关心正在使用的浏览器的确切版本时,这非常有用。

    对于ie 10和11:

    您可以使用js并在html中添加一个类来维护以下标准:

    或者使用类似bowser的库:

    或现代化功能检测:

    根据,以下是最佳解决方案,它也非常简单:

    function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
        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;
    }
    
    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 );
    }
    

    此函数将以整数形式返回IE主要版本号,如果浏览器不是Internet Explorer,则返回
    未定义的
    。与所有用户代理解决方案一样,用户代理欺骗是不可接受的(自第8版以来,这一直是IE的官方功能)


    我意识到我在这里参加聚会有点晚了,但我一直在研究一种简单的单行方式,以提供关于浏览器是否是IE以及从10到10的版本的反馈。我还没有为第11版编写代码,所以可能需要对此进行一些修改

    不管这是什么代码,它都是一个具有属性和方法的对象,依赖于对象检测,而不是抓取导航器对象(导航器对象有很大的缺陷,因为它可能被欺骗)

    用法是
    isIE.browser
    一个返回布尔值并依赖于条件注释的属性,方法
    isIE.detectedVersion()
    返回一个介于5和10之间的数字。我的假设是,任何低于6的东西,你都是在严肃的旧学校领域,你会有更多的牛肉
    if (isIE()) {
     // is IE
    } else {
     // Other browser
    }
    
    // ----------------------------------------------------------
    // A short snippet for detecting versions of IE in JavaScript
    // without resorting to user-agent sniffing
    // ----------------------------------------------------------
    // If you're not in IE (or IE version is less than 5) then:
    //     ie === undefined
    // If you're in IE (>=5) then you can determine which version:
    //     ie === 7; // IE7
    // Thus, to detect IE:
    //     if (ie) {}
    // And to detect the version:
    //     ie === 6 // IE6
    //     ie > 7 // IE8, IE9 ...
    //     ie < 9 // Anything less than IE9
    // ----------------------------------------------------------
    
    // UPDATE: Now using Live NodeList idea from @jdalton
    
    var ie = (function(){
    
        var undef,
            v = 3,
            div = document.createElement('div'),
            all = div.getElementsByTagName('i');
    
        while (
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
            all[0]
        );
    
        return v > 4 ? v : undef;
    
    }());
    
    if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
       // MSIE
    }
    
    var Browser = new function () {
        var self = this;
        var nav = navigator.userAgent.toLowerCase();
        if (nav.indexOf('msie') != -1) {
            self.ie = {
                version: toFloat(nav.split('msie')[1])
            };
        };
    };
    
    
    if(Browser.ie && Browser.ie.version > 9)
    {
        // do something
    }
    
      var ua = navigator.userAgent,
          doc = document.documentElement;
    
      if ((ua.match(/MSIE 10.0/i))) {
        doc.className = doc.className + " ie10";
    
      } else if((ua.match(/rv:11.0/i))){
        doc.className = doc.className + " ie11";
      }
    
    function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
        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;
    }
    
    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 );
    }
    
    function getIEVersion() {
        var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);
        return match ? parseInt(match[1]) : undefined;
    }
    
    var isIE = { browser:/*@cc_on!@*/false, detectedVersion: function () { return (typeof window.atob !== "undefined") ? 10 : (typeof document.addEventListener !== "undefined") ? 9 : (typeof document.querySelector !== "undefined") ? 8 : (typeof window.XMLHttpRequest !== "undefined") ? 7 : (typeof document.compatMode !== "undefined") ? 6 : 5; } };
    
    var isIE = { browser:/*@cc_on!@*/false, detectedVersion: function () { return (typeof window.atob !== "undefined") ? 10 : (typeof document.addEventListener !== "undefined") ? 9 : (typeof document.querySelector !== "undefined") ? 8 : (typeof window.XMLHttpRequest !== "undefined") ? 7 : (typeof document.compatMode !== "undefined") ? 6 : 5; } };
    
    /* testing IE */
    
    if (isIE.browser) {
      alert("This is an IE browser, with a detected version of : " + isIE.detectedVersion());
    }
    
    var uA = navigator.userAgent;
    var browser = null;
    var ieVersion = null;
    
    if (uA.indexOf('MSIE 6') >= 0) {
        browser = 'IE';
        ieVersion = 6;
    }
    if (uA.indexOf('MSIE 7') >= 0) {
        browser = 'IE';
        ieVersion = 7;
    }
    if (document.documentMode) { // as of IE8
        browser = 'IE';
        ieVersion = document.documentMode;
    }
    
    if (browser == 'IE' && ieVersion <= 9) 
        document.documentElement.className += ' ie9-';
    
    if (document.all && !document.addEventListener) {
        alert('IE8 or lower');
    }
    
    <!DOCTYPE HTML>
    <html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <script>if (navigator.appVersion.indexOf('Trident/') != -1) $("body").addClass("ie10");</script>
    </body>
    </html>
    
    var ie = (function (){
        if (window.ActiveXObject === undefined) return null; //Not IE
        if (!window.XMLHttpRequest) return 6;
        if (!document.querySelector) return 7;
        if (!document.addEventListener) return 8;
        if (!window.atob) return 9;
        if (!document.__proto__) return 10;
        return 11;
    })();
    
    return window.ActiveXObject === undefined ? null : !window.XMLHttpRequest ? 6 : !document.querySelector ? 7 : !document.addEventListener ? 8 : !window.atob ? 9 : !document.__proto__ ? 10 : 11;
    
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click on Try button to check IE Browser version.</p>
    
    <button onclick="getInternetExplorerVersion()">Try it</button>
    
    <p id="demo"></p>
    
    <script>
    function getInternetExplorerVersion() {
       var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");
            var rv = -1;
    
            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer, return version number
            {               
                if (isNaN(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))))) {
                    //For IE 11 >
                    if (navigator.appName == 'Netscape') {
                        var ua = navigator.userAgent;
                        var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
                        if (re.exec(ua) != null) {
                            rv = parseFloat(RegExp.$1);
                            alert(rv);
                        }
                    }
                    else {
                        alert('otherbrowser');
                    }
                }
                else {
                    //For < IE11
                    alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
                }
                return false;
            }}
    </script>
    
    </body>
    </html>
    
        <!DOCTYPE html>
        <!--[if lt IE 9]><html class="ie ie8"><![endif]-->
        <!--[if IE 9]><html class="ie ie9"><![endif]-->
        <!--[if (gt IE 9)|!(IE)]><!--><html><!--<![endif]-->
        <head>
            ...
            <!--[if lt IE 8]><meta http-equiv="Refresh" content="0;url=/error-browser.html"><![endif]--
            ...
        </head>
    
    var isIE9OrBelow = function()
    {
       return /MSIE\s/.test(navigator.userAgent) && parseFloat(navigator.appVersion.split("MSIE")[1]) < 10;
    }
    
    /**
     * documentMode is an IE-only property
     * http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
     */
    var msie = document.documentMode;
    
    if (msie < 9) {
        // code for IE < 9
    }
    
    <script>
       function isIE () {
           var myNav = navigator.userAgent.toLowerCase();
           return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
       }    
       var ua = window.navigator.userAgent;
       //Internet Explorer | if | 9-11
    
       if (isIE () == 9) {
           alert("Shut down this junk! | IE 9");
       } else if (isIE () == 10){
           alert("Shut down this junk! | IE 10");
       } else if (ua.indexOf("Trident/7.0") > 0) {
           alert("Shut down this junk! | IE 11");
       }else{
           alert("Thank god it's not IE!");
       }
    
    </script>
    
    // ----------------------------------------------------------
    // A short snippet for detecting versions of IE
    // ----------------------------------------------------------
    // If you're not in IE (or IE version is less than 5) then:
    //     ie === undefined
    // Thus, to detect IE:
    //     if (ie) {}
    // And to detect the version:
    //     ie === 6 // IE6
    //     ie > 7 // IE8, IE9 ...
    // ----------------------------------------------------------
    var ie = (function(){
        var v = 3,
            div = document.createElement('div'),
            all = div.getElementsByTagName('i');
    
        while (
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
            all[0]
        );
        if (v <= 4) { // Check for IE>9 using user agent
            var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:|Edge\/)(\d+)/);
            v = match ? parseInt(match[1]) : undefined;
        }
        return v;
    }());
    
        if (ie) {
            document.documentElement.className += ' ie' + ie;
            if (ie < 9)
                document.documentElement.className += ' ieLT9';
        }
    
    if (!document.addEventListener) {
        // ie8
    } else if (!window.btoa) {
        // ie9
    }
    // others
    
    !!navigator.userAgent.match(/msie\s[5-8]/i)
    
    _.isIE();        // Any version of IE?
    _.isIE(9);       // IE 9?
    _.isIE([7,8,9]); // IE 7, 8 or 9?
    
    //   IE 10: ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'; 
    //   IE 11: ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'; 
    // Edge 12: ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'; 
    // Edge 13: ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586'; 
    
    var isIE = navigator.userAgent.match(/MSIE|Trident|Edge/)
    var IEVersion = ((navigator.userAgent.match(/(?:MSIE |Trident.*rv:|Edge\/)(\d+(\.\d+)?)/)) || []) [1]