Get browser version of IE using Javascript [duplicate]
Asked Answered
S

4

5

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?

Sybil answered 18/12, 2009 at 5:10 Comment(0)
S
4

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

Shrug answered 18/12, 2009 at 5:14 Comment(3)
Its really a good technique. But is there any performance issues??Sybil
I'm pretty sure there are not. This is, as I understand it, a "best practice".Shrug
And it's certainly faster than anything involving regular expressions, and probably string slicing too.Shrug
A
7

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])
Affectation answered 18/12, 2009 at 5:27 Comment(0)
S
4

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

Shrug answered 18/12, 2009 at 5:14 Comment(3)
Its really a good technique. But is there any performance issues??Sybil
I'm pretty sure there are not. This is, as I understand it, a "best practice".Shrug
And it's certainly faster than anything involving regular expressions, and probably string slicing too.Shrug
S
2

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.

Seeress answered 25/6, 2013 at 13:24 Comment(0)
C
-2

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>
Cancer answered 18/12, 2009 at 5:13 Comment(1)
Where from the 'Event.observe'?Sybil

© 2022 - 2024 — McMap. All rights reserved.