jQuery/Javascript 在没有插件的情况下检测操作系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7044944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:35:26  来源:igfitidea点击:

jQuery/Javascript to detect OS without a plugin?

javascriptjqueryoperating-system

提问by Tim Withers

I am looking for a way to detect the OS for a downloads page using jQuery or Javascript to recommend specific files for Mac vs Windows. I was hoping to do it without adding another plugin to my page.

我正在寻找一种方法来检测下载页面的操作系统,使用 jQuery 或 Javascript 为 Mac 和 Windows 推荐特定文件。我希望在不向我的页面添加另一个插件的情况下做到这一点。

回答by RobB

Try:

尝试:

var os = navigator.platform;

var os = navigator.platform;

Then handle the os variable accordingly for your result.

然后根据您的结果相应地处理 os 变量。

You can also loop through each object of the navigatorobject to help get you more familiarized with the objects:

您还可以遍历对象的每个对象,navigator以帮助您更熟悉这些对象:

<script type="text/javascript">
for(var i in navigator){
    console.log(i+"="+navigator[i]+'<br>');
}
</script>

回答by Shawn

Plain JavaScript might be all you need.

纯 JavaScript 可能就是您所需要的。

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

As Nick suggested you could use navigator.platformas well.

正如尼克建议你也可以使用navigator.platform

回答by Vitim.us

As far as I know the platformis the less spoofed property on the navigator Object. You can use this to get booleans.

据我所知,这platform是导航器对象上较少被欺骗的属性。您可以使用它来获取布尔值。

var isMac = navigator.platform.toUpperCase().indexOf('MAC')!==-1;
var isWindows = navigator.platform.toUpperCase().indexOf('WIN')!==-1;
var isLinux = navigator.platform.toUpperCase().indexOf('LINUX')!==-1;

If you need to differentiate Macs between the old PowerPc and new Intel.

如果您需要区分旧的 PowerPc 和新的 Intel 之间的 Mac。

var isMacPpc=navigator.platform==="MacPPC";
var isMacIntel=navigator.platform==="MacIntel";

https://developer.mozilla.org/en/DOM/window.navigator.platform

https://developer.mozilla.org/en/DOM/window.navigator.platform

回答by davecoulter

Try:

尝试:

alert(navigator.appVersion);

That should give you a string that you can parse for the OS.

这应该为您提供一个可以为操作系统解析的字符串。

回答by gaby de wilde

<script>

osName = 'Unknown';

function nav(x, y, z) {
    z = z || y;
    if (navigator[x] && navigator[x].indexOf(y) !== -1) {
        osName = z;
    }
}

/*   navigator     value     download  */
nav( "appVersion", "X11",    "UNIX"    );
nav( "appVersion", "Mac",    "MacOS"   );
nav( "appVersion", "Linux"             );
nav( "userAgent",  "Linux"             );
nav( "platform",   "Linux"             );
nav( "appVersion", "Win",    "Windows" );
nav( "userAgent",  "Windows"           );
nav( "platform",   "Win",    "Windows" );
nav( "oscpu",      "Windows"           );

document.getElementById("download"+osName).className = "knownOS";

</script>

Make sure the right download link is easy to find, but without hiding the other OS links. The people might still want those.

确保容易找到正确的下载链接,但不隐藏其他操作系统链接。人们可能仍然想要那些。

<style>

#downloadUNIX, #downloadMacOS, #downloadLinux, #downloadWindows {
    color:#6D94F2;
    line-height:35px;
    margin:24px 0 24px 0;
    padding:10px;
}
.knownOS {
    background-color:#F7ECAD !important;
    border:2px solid #E8913A;
    color:#133CC4 !important;
    font-weight:bold;
}

</style>

And some html

还有一些 html

<ul>
    <li><a id="downloadUNIX"    href="unix Link Here"   >Download Napster-9000 for UNIX</a></li>
    <li><a id="downloadWindows" href="windows Link Here">Download Napster-9000 for Windows</a></li>
    <li><a id="downloadMacOS"   href="mac os link here" >Download Napster-9000 for OS X</a></li>
    <li><a id="downloadLinux"   href="linux Link Here"  >Download Napster-9000 for Linux</a></li>
</ul>

Now the user may disable or block javascripts if he wants. The links will still be there, as opposed to writing the links with Javascript, which requires javascript to work.

现在,用户可以根据需要禁用或阻止 javascripts。链接仍然存在,而不是用 Javascript 编写链接,这需要 javascript 才能工作。

Here is a fiddle

这是一个小提琴

http://jsfiddle.net/7fmJb/

http://jsfiddle.net/7fmJb/

回答by Rohan Varma

You can use this function inside document ready function. Add the code inside function for your event.

您可以在文档就绪功能中使用此功能。为您的事件添加函数内的代码。

function checkOperatingSystem()
{
    var  userAgent = navigator.userAgent || navigator.vendor || window.opera;

    //Check mobile device is Android
    if (/android/i.test(userAgent)) {
        //Add your Code here
    }

    //Check mobile device is IOS
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        //Add your Code here
    }

    //Check device os is Windows (For Laptop and PC)
    if (navigator.appVersion.indexOf("Win")!=-1)
    {
        //Add your Code here
    }

    //Check device os is MAC (For Laptop and PC)
    if (navigator.appVersion.indexOf("Mac")!=-1)
    {
        //Add your Code here
    }  
}

回答by rahim.nagori

I think this might help:

我认为这可能会有所帮助:

navigator.appVersion

Try consoling it in your JS file. For example:

尝试在您的 JS 文件中安慰它。例如:

console.log(navigator.appVersion);

You'll get most of the info regarding OS.

您将获得有关操作系统的大部分信息。