2

I'm working on correctly finding client browser version in the code below. Everything works but IE11 version number. I keep getting a value of 5. The browser shows correctly for all version that I tested but IE11. I've tried a few things but I'm stumped. Can anyone help me with what I'm missing, Thank you.

// BrowserInformation
    vm.objappVersion = navigator.appVersion;
    vm.objAgent = navigator.userAgent; 
    vm.objbrowserName = navigator.appName; 
    vm.objfullVersion = ''+parseFloat(navigator.appVersion); 
    vm.objBrMajorVersion = parseInt(navigator.appVersion,10); 
    vm.objOffsetName = '';
    vm.objOffsetVersion = '';
    vm.ix;
    // In Chrome 
    if ((vm.objOffsetVersion = vm.objAgent.indexOf("Chrome")) != -1) {
        vm.objbrowserName = "Chrome"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 7);
    }
        // In IE11
    else if ((vm.objOffsetVersion = vm.objAgent.indexOf("rv")) != -1) {
        vm.objbrowserName = "Microsoft Internet Explorer Version 11"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 10);
    }
        // In Microsoft internet explorer all other versions
    else if ((vm.objOffsetVersion = vm.objAgent.indexOf("MSIE")) != -1) {
        vm.objbrowserName = "Microsoft Internet Explorer"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 5);
    }
        // In Firefox 
    else if ((vm.objOffsetVersion = vm.objAgent.indexOf("Firefox")) != -1) {
        vm.objbrowserName = "Firefox";
    }
        // In Safari
    else if ((vm.objOffsetVersion = vm.objAgent.indexOf("Safari")) != -1) {
        vm.objbrowserName = "Safari"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 7);
        if ((vm.objOffsetVersion = vm.objAgent.indexOf("Version")) != -1) vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 8);
    }
        // For other browser "name/version" is at the end of userAgent 
    else if ((vm.objOffsetName = vm.objAgent.lastIndexOf(' ') + 1) < (vm.objOffsetVersion = vm.objAgent.lastIndexOf('/'))) {
        vm.objbrowserName = vm.objAgent.substring(vm.objOffsetName, vm.objOffsetVersion); vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 1);
        if (vm.objbrowserName.toLowerCase() == vm.objbrowserName.toUpperCase()) { vm.objbrowserName = navigator.appName; }
    }
    // trimming the fullVersion string at semicolon/space if present 
    if ((vm.ix = vm.objfullVersion.indexOf(";")) != -1)
        vm.objfullVersion = vm.objfullVersion.substring(0, vm.ix);
    if ((vm.ix = vm.objfullVersion.indexOf(" ")) != -1)
        vm.objfullVersion = vm.objfullVersion.substring(0, vm.ix);
        vm.objBrMajorVersion = parseInt('' + vm.objfullVersion, 10);
    if (isNaN(vm.objBrMajorVersion)) {
        vm.objfullVersion = '' + parseFloat(navigator.appVersion);
        vm.objBrMajorVersion = parseInt(navigator.appVersion, 10);
    }
  • 2
    May I ask [why] you are sniffing browser versions? Also, what is the value of `navigator.userAgent` in IE 11 for you? – Sampson Jan 14 '16 at 18:39
  • @Sampson The version number would be for support reasons. navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; GWX:RESERVED; TNJB; rv:11.0) like Gecko" – fe4rL3ssC0der Jan 14 '16 at 18:48
  • Perhaps, if `.indexOf("rv")` is greater than `-1`, you should `ua.match(/rv:(\d+)/)` and grab the value at index 1 of the resulting array? – Sampson Jan 14 '16 at 18:55

1 Answers1

1

Here is an update of your script that works

vm.objappVersion = navigator.appVersion;
vm.objAgent = navigator.userAgent; 
vm.objbrowserName = navigator.appName; 
vm.objfullVersion = ''+parseFloat(navigator.appVersion); 
vm.objBrMajorVersion = parseInt(navigator.appVersion,10); 
vm.objOffsetName = '';
vm.objOffsetVersion = '';
vm.ix;
// In Chrome 
if ((vm.objOffsetVersion = vm.objAgent.indexOf("Chrome")) != -1) {
    vm.objbrowserName = "Chrome"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 7);
}
    // In IE11
else if ((vm.objOffsetVersion = vm.objAgent.indexOf("rv")) != -1) {
    vm.objbrowserName = "Microsoft Internet Explorer Version 11"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 2);        
}
    // In Microsoft internet explorer all other versions
else if ((vm.objOffsetVersion = vm.objAgent.indexOf("MSIE")) != -1) {
    vm.objbrowserName = "Microsoft Internet Explorer"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 5);
}
    // In Firefox 
else if ((vm.objOffsetVersion = vm.objAgent.indexOf("Firefox")) != -1) {
    vm.objbrowserName = "Firefox";
}
    // In Safari
else if ((vm.objOffsetVersion = vm.objAgent.indexOf("Safari")) != -1) {
    vm.objbrowserName = "Safari"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 7);
    if ((vm.objOffsetVersion = vm.objAgent.indexOf("Version")) != -1) vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 8);
}
    // For other browser "name/version" is at the end of userAgent 
else if ((vm.objOffsetName = vm.objAgent.lastIndexOf(' ') + 1) < (vm.objOffsetVersion = vm.objAgent.lastIndexOf('/'))) {
    vm.objbrowserName = vm.objAgent.substring(vm.objOffsetName, vm.objOffsetVersion); vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 1);
    if (vm.objbrowserName.toLowerCase() == vm.objbrowserName.toUpperCase()) { vm.objbrowserName = navigator.appName; }
}

// trimming the fullVersion string at semicolon/space if present 
if ((vm.ix = vm.objfullVersion.indexOf(";")) != -1) {
    vm.objfullVersion = vm.objfullVersion.substring(0, vm.ix);
}
if ((vm.ix = vm.objfullVersion.indexOf(":")) != -1) {
    vm.ix = vm.objfullVersion.indexOf(")");
    vm.objfullVersion = vm.objfullVersion.substring(1, vm.ix);
    vm.objBrMajorVersion = parseInt('' + vm.objfullVersion, 10);
}
if ((vm.ix = vm.objfullVersion.indexOf(" ")) != -1) {
    vm.objfullVersion = vm.objfullVersion.substring(0, vm.ix);
    vm.objBrMajorVersion = parseInt('' + vm.objfullVersion, 10);
}
if (isNaN(vm.objBrMajorVersion)) {
    vm.objfullVersion = '' + parseFloat(navigator.appVersion);
    vm.objBrMajorVersion = parseInt(navigator.appVersion, 10);
}

Changed the following line:

vm.objbrowserName = "Microsoft Internet Explorer Version 11"; vm.objfullVersion = vm.objAgent.substring(vm.objOffsetVersion + 2);

And added these lines

if ((vm.ix = vm.objfullVersion.indexOf(":")) != -1) {
    vm.ix = vm.objfullVersion.indexOf(")");
    vm.objfullVersion = vm.objfullVersion.substring(1, vm.ix);
    vm.objBrMajorVersion = parseInt('' + vm.objfullVersion, 10);
}

Side note: I do recommend though, to be careful sniffing/detecting like that, as it can easily go wrong.

Asons
  • 84,923
  • 12
  • 110
  • 165
  • This is for a private cloud based application, but information will be used for the case that we need to troubleshoot an issue. As of right now we are not supporting certain older versions of browsers at this time. This will help the client provide us with correct version. I also tried your suggestion and still the same result. – fe4rL3ssC0der Jan 14 '16 at 19:06
  • @Adam What is `vm.objfullVersion` value/string for IE11? – Asons Jan 14 '16 at 19:12
  • @Adam And what value has `vm.objAgent` for IE11? – Asons Jan 14 '16 at 19:13
  • vm.objfullVersion = 5 vm.objAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; GWX:RESERVED; TNJB; rv:11.0) like Gecko" – fe4rL3ssC0der Jan 14 '16 at 19:34
  • For the time being I'm just going to set it to 11 manually. I have other projects that I need to work on. I will check back to try things. I want to thank everyone for helping. If I find a fix I will make sure to post it. – fe4rL3ssC0der Jan 14 '16 at 19:55
  • @Adam I found the error .. will update my answer soon – Asons Jan 14 '16 at 19:57
  • @Adam Updated my answer with a working script. – Asons Jan 14 '16 at 20:12
  • Thank you very much for the help that worked for me. – fe4rL3ssC0der Jan 14 '16 at 20:19