小编典典

如何使用JavaScript检测我的浏览器版本和操作系统?

javascript

我尝试使用下面的代码,但仅在Chrome和Mozilla中显示结果,而在IE6中不起作用。

<div id="example"></div>

<script type="text/javascript">

txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";

document.getElementById("example").innerHTML=txt;

</script>

输出:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows)

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0

我只需要获取版本“ Firefox / 12.0”。


阅读 278

收藏
2020-04-25

共1个答案

小编典典

检测浏览器的详细信息:

var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;

// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);

majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}

document.write(''
 +'Browser name  = '+browserName+'<br>'
 +'Full version  = '+fullVersion+'<br>'
 +'Major version = '+majorVersion+'<br>'
 +'navigator.appName = '+navigator.appName+'<br>'
 +'navigator.userAgent = '+navigator.userAgent+'<br>'
)

源JavaScript:浏览器名称

正在检测操作系统:

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

源JavaScript:OS检测。

    var nVer = navigator.appVersion;

    var nAgt = navigator.userAgent;

    var browserName  = navigator.appName;

    var fullVersion  = ''+parseFloat(navigator.appVersion);

    var majorVersion = parseInt(navigator.appVersion,10);

    var nameOffset,verOffset,ix;



    // In Opera, the true version is after "Opera" or after "Version"

    if ((verOffset=nAgt.indexOf("Opera"))!=-1) {

     browserName = "Opera";

     fullVersion = nAgt.substring(verOffset+6);

     if ((verOffset=nAgt.indexOf("Version"))!=-1)

       fullVersion = nAgt.substring(verOffset+8);

    }

    // In MSIE, the true version is after "MSIE" in userAgent

    else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {

     browserName = "Microsoft Internet Explorer";

     fullVersion = nAgt.substring(verOffset+5);

    }

    // In Chrome, the true version is after "Chrome"

    else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {

     browserName = "Chrome";

     fullVersion = nAgt.substring(verOffset+7);

    }

    // In Safari, the true version is after "Safari" or after "Version"

    else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {

     browserName = "Safari";

     fullVersion = nAgt.substring(verOffset+7);

     if ((verOffset=nAgt.indexOf("Version"))!=-1)

       fullVersion = nAgt.substring(verOffset+8);

    }

    // In Firefox, the true version is after "Firefox"

    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {

     browserName = "Firefox";

     fullVersion = nAgt.substring(verOffset+8);

    }

    // In most other browsers, "name/version" is at the end of userAgent

    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <

              (verOffset=nAgt.lastIndexOf('/')) )

    {

     browserName = nAgt.substring(nameOffset,verOffset);

     fullVersion = nAgt.substring(verOffset+1);

     if (browserName.toLowerCase()==browserName.toUpperCase()) {

      browserName = navigator.appName;

     }

    }

    // trim the fullVersion string at semicolon/space if present

    if ((ix=fullVersion.indexOf(";"))!=-1)

       fullVersion=fullVersion.substring(0,ix);

    if ((ix=fullVersion.indexOf(" "))!=-1)

       fullVersion=fullVersion.substring(0,ix);



    majorVersion = parseInt(''+fullVersion,10);

    if (isNaN(majorVersion)) {

     fullVersion  = ''+parseFloat(navigator.appVersion);

     majorVersion = parseInt(navigator.appVersion,10);

    }



    document.write(''

     +'Browser name  = '+browserName+'<br>'

     +'Full version  = '+fullVersion+'<br>'

     +'Major version = '+majorVersion+'<br>'

     +'navigator.appName = '+navigator.appName+'<br>'

     +'navigator.userAgent = '+navigator.userAgent+'<br>'

    )



    // This script sets OSName variable as follows:

    // "Windows"    for all versions of Windows

    // "MacOS"      for all versions of Macintosh OS

    // "Linux"      for all versions of Linux

    // "UNIX"       for all other UNIX flavors

    // "Unknown OS" indicates failure to detect the OS



    var OSName="Unknown OS";

    if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";

    if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";

    if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";

    if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";



    document.write('Your OS: '+OSName);
2020-04-25