Get browser version of IE using Javascript [duplicate]

自古美人都是妖i 提交于 2019-12-10 14:51:53

问题


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

    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?


回答1:


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




回答2:


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

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

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

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

If you only want 6.0 you could do like

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



回答3:


The below function isIE returns IE version if IE is detected else returns 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 here by weroro.




回答4:


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>


来源:https://stackoverflow.com/questions/1926394/get-browser-version-of-ie-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!