89

我想提供正确版本的下载。我拥有的版本是:

  • 32 位 Windows
  • 64 位 Windows
  • Linux

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

用户可能正在使用奇怪的浏览器——IE 和 Firefox 很常见,而且我们可能在某个地方有一个 Opera 用户;也可能是 Chrome 用户。我知道 64 位 Windows 7 附带 32 位和 64 位版本的 IE,我想将我下载的 64 位版本都发送给他们。

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

4

12 回答 12

57

试试这个,在用户代理字符串中查找 WOW64(64 位上的 32 位)或 Win64(本机 64 位)。

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

于 2011-07-28T22:33:18.693 回答
42

我做了一些测试。以下是结果,希望对您有所帮助:

64 位 MacOS + 64 位 Safari 或 32 位 Chrome:
window.navigator.platform=MacIntel

32 位 Windows + Safari:
window.navigator.platform=Win32

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

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

64 位 Windows + 32 Firefox(或 Chrome):
window.navigator.platform=Win32

32 位 linux mint (i686) + 火狐:
window.navigator.platform=Linux i686

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

64 位 Ubuntu + 64 位主显节:
window.navigator.platform=Linux x86_64

到目前为止,我已经使用了这段代码:

deployJava.isWin64OS = function() {
    return navigator.userAgent.indexOf('WOW64')>-1 || window.navigator.platform=='Win64';
};
于 2011-06-07T14:45:00.680 回答
32

分析大约14000 个唯一的用户代理从这里开始),我想出了以下要查找的字符串:

  • x86_64
  • x86-64
  • Win64
  • x64; (注意分号!没有它,您将有误报。)
  • amd64
  • AMD64
  • 哇64
  • x64_64

此外,尽管它们具有不同的指令集并且与 Intel x86_64 不兼容,但您可能需要检测以下内容:

  • ia64
  • sparc64
  • ppc64
  • IRIX64

但请注意,不要只查找包含“64”甚至“x64”的任何内容。Chrome 的内部版本号、蜘蛛/机器人、库、.NET 版本、分辨率等也可能包含字符串“x64”,但仍是 32 位(或其他)操作系统。

请注意,您可以不区分大小写地搜索所有这些字符串。

我在 ARM 上找不到任何东西。也许是其他人?请编辑,这是一个社区维基。

于 2012-12-04T18:23:54.823 回答
24

您可以检查window.navigator.platformwindow.navigator.cpuClass

我不确定你的情况,但我会考虑做大多数其他网站所做的事情,让用户选择他们获得的下载。他们可能将它下载到另一台机器上,放在闪存设备上,或者只是希望 32 位版本在他们的 64 位机器上运行。不管什么原因,我宁愿选择。

于 2009-11-16T12:42:53.273 回答
19

最可靠的解决方案是创建一个 32 位加载器应用程序来检测架构,然后下载并安装应用程序的适当版本。

我已经检查了 RC 和 Pino 的其他两个答案。由于与您建议的问题相同,它们都不起作用 - 64 位 Windows 上的 32 位 IE 会错误地将平台识别为 32 位。由于大多数人在 64 位 Windows 上运行 32 位 IE(许多插件,例如 Flash 在 64 位中不可用),因此会有很多不准确的识别

于 2009-11-16T12:45:39.693 回答
9

正如您所说,浏览器可能是 32 位版本,而操作系统可能是 64 位版本,并不是 100% 确定的。

要检测浏览器,请尝试以下代码:

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

处理器:ia64

对于 IE。

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

商业产品:https ://www.cyscape.com/showbrow.aspx

于 2009-11-16T12:38:32.877 回答
5

对于带有 64 位 IE 的 64 位 Windows,window.navigator.platform将是“Win64”和window.navigator.cpuClass“x64”。

对于带有 32 位 IE 的 64 位 Windows,window.navigator.platform将为“Win32”和window.navigator.cpuClass“x86”。

对于 32 位 Windows,window.navigator.platform将是“Win32”并且window.navigator.cpuClass未定义(我认为)。

-

资料来源:我制作了一个应用程序,它使用 JavaScript 来确定某人使用的是 32 位还是 64 位处理器您可以在 GitHub 上查看代码

于 2014-07-29T03:03:19.993 回答
3

我将上面漂亮搜索的结果恢复到这些 JS 函数中。希望他们可以帮助这里的每个人快速响应他们的需求(以及我的需求!)

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

测试它:

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

谢谢大家!

于 2015-09-28T10:26:17.270 回答
2

我使用了以下代码:

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

它在任何地方都可以使用,除了 Mac 电脑。不幸的是,似乎无法通过 JavaScript 获取该信息 :(。不过,还有一个技巧可以在那里完成。因为 Adob​​e 在 x64 浏览器上不支持 Flash 播放器,您可以尝试检测它。如果检测成功,比它肯定是32位浏览器,如果不是,比它没有flash插件的32位浏览器或64位浏览器。因为Flash播放器的渗透率相当大(见http://www.adobe.com/products/player_census/ flashplayer/version_penetration.html),这应该足以至少在 Mac 下检测 x32 浏览器。

于 2011-05-31T12:28:04.427 回答
1

适用于任何 Internet Explorer 浏览器的 64 位 Windows 上的 64 位 IE

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

}
于 2011-05-30T11:57:25.330 回答
1

window.navigator.cpuClass 和 window.navigator.platform 都返回浏览器平台。不是系统平台。因此,如果您在 64 位系统上运行 32 位浏览器,那么这两个变量都将返回 32 位。这是不正确的。

于 2014-04-15T12:42:49.933 回答
1

我发现了这个老问题,并想用我发现的最近的开源库进行更新:https ://github.com/faisalman/ua-parser-js

根据文档,方法getCPU() returns { architecture: '' },具有以下可能的值:68k, amd64, arm, arm64, avr, ia32, ia64, irix, irix64, mips, mips64, pa-risc, ppc, sparc, sparc64.

于 2016-04-22T00:21:47.683 回答