Javascript检测IE11版本

时间:2016-01-14 18:37:47

标签: javascript

我正在努力在下面的代码中正确查找客户端浏览器版本。一切正常但IE11版本号。我一直得到值5.浏览器正确显示我测试的所有版本但IE11。我尝试了一些事情,但我很难过。能不能帮助我解决我失踪的问题,谢谢。

// 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);
    }

1 个答案:

答案 0 :(得分:1)

以下是适用于您的脚本的更新

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);
}

更改了以下行:

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

并添加了这些行

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);
}

旁注: 我建议,但要小心嗅探/检测,因为它很容易出错。