这基于 jQuery – detecting the operating system and operating system version 我只想测试Windows或Linux或Apple平台.这是对的吗? var os = (function () { var ua = navigator.userAgent.toLowerCase();return { isWindows: /(window
我只想测试Windows或Linux或Apple平台.这是对的吗?
var os = (function () { var ua = navigator.userAgent.toLowerCase(); return { isWindows: /(windows|win95|win32|win64)/.test(ua), isLinux: /(linux|freebsd|ssl-mm)/.test(ua), isApple: /(mac os|mac_powerpc)/.test(ua) }; }());
测试:http://jsfiddle.net/k5mGC/4/
是否有完整的用户代理结果列表?我只找到了像http://user-agents.my-addr.com/user_agent_request/user_agent_examples-and-user_agent_types.php这样的示例列表
您也可以使用navigator.appVersion.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"; alert("You are using " + OSName);
DEMO