从用户代理或 Javascript 检测64位或32位 Windows?

我想提供正确的下载版本,我有以下版本:

  • 32位 Windows
  • 64位 Windows
  • Linux

使用 User Agent 字段检测 Linux 很容易; 但是是否可以可靠地确定 Windows 是32位还是64位?

用户可能正在使用奇怪的浏览器—— IE 和 Firefox 很常见,我们可能有一个 Opera 用户; 也许还有一个 Chrome 用户。我知道64位的 Windows 7附带了32位和64位版本的 IE,我想把我下载的64位版本发给他们。

(编辑添加: 我知道我应该提供所有的选项,我会。但是 人们不看选项。因此,我想提供正确的默认下载,以提高可用性。当然,如果我做对了,这是有帮助的,但是如果我做错了,这是非常没有帮助的。从目前的答案来看,似乎没有一种可靠的方法可以做到这一点)。

72302 次浏览

Not with 100% certainty as you say the browser could be a 32bit version while the OS a 64bit.

To detect the browser, please try the following code:

<script language=javascript>
<!--
document.write("CPU :"+window.navigator.cpuClass);
//-->
</script>

CPU : ia64

For IE.

http://msdn.microsoft.com/en-us/library/ms531090%28VS.85%29.aspx

Commercial Product : https://www.cyscape.com/showbrow.aspx

You can check the window.navigator.platform and the window.navigator.cpuClass.

I'm not sure your situation, but I would consider just doing what most other sites do and let the user choose which download they get. They could be downloading it for another machine, to put on a flash device, or just may simply want the 32-bit version to run on their 64-bit box. Whatever reason, I would rather have the choice.

The most reliable solution would be to create a 32bit loader application that detects the architecture and then downloads and installs the appropriate version of your application.

I've checked the other two answers from RC and Pino. They both do not work because of the same problem as you suggest - 32-bit IE on 64-bit Windows will wrongly identify the platform as 32-bit. As most people run 32-bit IE on 64-bit Windows (many plugins e.g. Flash are not available in 64-bit), there will be a lot of innacurate identifications

Lee

64-bit IE on 64-bit Windows for any Internet Explorer browser

if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Win64") != -1 && navigator.userAgent.indexOf("x64") != -1){


alert("This is 64 bit browser");


}
else {


alert("Not 64 bit browser");


}

I used following code:

var is32BitBrowser = true;
if( window.navigator.cpuClass != null && window.navigator.cpuClass.toLowerCase() == "x64" )
is32BitBrowser = false;
if( window.navigator.platform.toLowerCase() == "win64" )
is32BitBrowser = false;

It worked everywhere expect Mac computers. And unfortunately seems that it's not possible to get that information via JavaScript :(. However one more trick could be done there. Because Adobe didn't support flash player on x64 browsers, you can just try to detect it. If detection is successful, than it is definitely 32 bit browser, if no, than it's 32 bit browser without flash plugin or it's 64 bit browser. Because penetration rate of Flash player is quite huge(see http://www.adobe.com/products/player_census/flashplayer/version_penetration.html), this should be good enough to at least detect x32 browser under Mac.

I've done some tests. Here are the results, hope it helps:

64 bit MacOS + 64 bit Safari or 32 bit Chrome:
window.navigator.platform=MacIntel


32 bit windows + safari:
window.navigator.platform=Win32


64 bit Windows + 64 bit IE:
window.navigator.platform=Win64
window.navigator.cpuClass=x64


64 bit Windows + 32 bit IE:
window.navigator.platform=Win32
window.navigator.cpuClass=x86


64 bit Windows + 32 Firefox (or Chrome):
window.navigator.platform=Win32


32 bit linux mint (i686) + firefox:
window.navigator.platform=Linux i686


64 bit Ubuntu (x86_64) + 32 bit Chrome:
window.navigator.platform=Linux i686


64 bit Ubuntu + 64 bit Epiphany:
window.navigator.platform=Linux x86_64

So far i've used this code:

deployJava.isWin64OS = function() {
return navigator.userAgent.indexOf('WOW64')>-1 || window.navigator.platform=='Win64';
};

Try this, looks for WOW64 (32-bit on 64-bit) or Win64 (native 64-bit) in the user-agent string.

    if (navigator.userAgent.indexOf("WOW64") != -1 ||
navigator.userAgent.indexOf("Win64") != -1 ){
alert("This is a 64 bit OS");
} else {
alert("Not a 64 bit OS");
}

Analysing around 14000 unique user-agents (from here), I've come up with the following strings to look for:

  • x86_64
  • x86-64
  • Win64
  • x64; (Mind the semicolon! Without it you will have false-positives.)
  • amd64
  • AMD64
  • WOW64
  • x64_64

Additionally, although they have different instruction sets and are not compatible with Intel x86_64, you may want to detect the following:

  • ia64
  • sparc64
  • ppc64
  • IRIX64

Beware though, don't just look for anything containing "64" or even "x64". Chrome's build numbers, spiders/bots, libraries, .NET versions, resolutions, etc. may also contain the string "x64" while still being a 32-bits (or other) OS.

Note that you can search for all those strings case-insensitively.

I have not been able to find anything on ARM. Perhaps someone else? Please edit, it is a community wiki.

Both window.navigator.cpuClass and window.navigator.platform return the browser platform. Not the system platform. So if you are running a 32-bit browser on a 64-bit system then both varibales would return 32-bit. Which would be incorrect.

For 64-bit Windows with 64-bit IE window.navigator.platform will be "Win64" and window.navigator.cpuClass will be "x64".

For 64-bit Windows with 32-bit IE, window.navigator.platform will be "Win32" and window.navigator.cpuClass will be "x86".

For 32-bit Windows, window.navigator.platform will be "Win32" and window.navigator.cpuClass will be undefined (I think).

-

Source: I made an app that uses JavaScript to determine if someone is using a 32 bit or 64 bit processor. You can see the code here on GitHub.

I resumed the results of the nice search above into these JS functions. Hope they can help everybody here to catch up a quick response to their needs (and, as well, to mine too !)

function get_bits_system_architecture()
{
var _to_check = [] ;
if ( window.navigator.cpuClass ) _to_check.push( ( window.navigator.cpuClass + "" ).toLowerCase() ) ;
if ( window.navigator.platform ) _to_check.push( ( window.navigator.platform + "" ).toLowerCase() ) ;
if ( navigator.userAgent ) _to_check.push( ( navigator.userAgent + "" ).toLowerCase() ) ;


var _64bits_signatures = [ "x86_64", "x86-64", "Win64", "x64;", "amd64", "AMD64", "WOW64", "x64_64", "ia64", "sparc64", "ppc64", "IRIX64" ] ;
var _bits = 32, _i, _c ;
outer_loop:
for( var _c = 0 ; _c < _to_check.length ; _c++ )
{
for( _i = 0 ; _i < _64bits_signatures.length ; _i++ )
{
if ( _to_check[_c].indexOf( _64bits_signatures[_i].toLowerCase() ) != -1 )
{
_bits = 64 ;
break outer_loop;
}
}
}
return _bits ;
}




function is_32bits_architecture() { return get_bits_system_architecture() == 32 ? 1 : 0 ; }
function is_64bits_architecture() { return get_bits_system_architecture() == 64 ? 1 : 0 ; }

Test it:

document.write( "Which is my current bits system architecture ? " + get_bits_system_architecture() + "<br>" );


document.write( "Is it 32 bits ? " + ( is_32bits_architecture() ? "YES" : "NO" ) + "<br>" );


document.write( "Is it 64 bits ? " + ( is_64bits_architecture() ? "YES" : "NO" ) );

Thanks to everyone!

I've found this old question and thought updating with a recent open source library I found: https://github.com/faisalman/ua-parser-js

According to the docs, the method getCPU() returns { architecture: '' }, with the following possible values: 68k, amd64, arm, arm64, avr, ia32, ia64, irix, irix64, mips, mips64, pa-risc, ppc, sparc, sparc64.