使用 Javascript 获取浏览器版本的 IE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1926394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:55:50  来源:igfitidea点击:

Get browser version of IE using Javascript

javascriptinternet-explorerversion-detection

提问by Sauron

I am using the following code to get the version of IE in a system.

我正在使用以下代码来获取系统中 IE 的版本。

    var browser = navigator.appName;
    var b_version = navigator.appVersion;
    var version = parseFloat(b_version);
    alert(version);

But the version always get is 4 in IE^ and IE7. How can I get the exact version?

但是在IE^和IE7中总是得到的版本是4。我怎样才能得到确切的版本?

采纳答案by Avi Flax

It's generally not a good idea to use version detection — in fact, even browser detection isn't recommended! Instead, try object detection.

使用版本检测通常不是一个好主意——事实上,甚至不推荐使用浏览器检测!相反,请尝试对象检测

回答by YOU

You got 4 because of navigator.appVersion strings starts with 4.0 like this.

你得到 4 因为 navigator.appVersion 字符串以 4.0 开头,就像这样。

4.0 (compatible; MSIE 6.0; Windows NT 5.0; ...)

If you do like this, you will get MSIE 6.0for above case

如果你这样做,你会得到MSIE 6.0上述情况

alert(navigator.appVersion.match(/MSIE [\d.]+/))

If you only want 6.0you could do like

如果你只想要6.0你可以像

alert(navigator.appVersion.match(/MSIE ([\d.]+)/)[1])

回答by Binod Kalathil

The below function isIEreturns IE version if IE is detected else returns FALSE

如果检测到 IE,则以下函数isIE返回 IE 版本,否则返回FALSE

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

This is based on the answer hereby weroro.

这是基于weroro在此处的答案。

回答by Chirag Patel

Try something like this:

尝试这样的事情:

<script language="javascript">
        Event.observe(window, 'load', function() {
            var el = $("browserName");
            var BO = detectBrowser();
            if(BO.ie6){
                el.innerHTML = "<b>We do not support IE6. Please click <a href=\"http://www.microsoft.com/windows/downloads/ie/getitnow.mspx\">here</a> to upgrade.</b>";
            }else{
                el.innerHTML = "<b>Thank You for not running IE6.</b>";
            }
        });
</script>