在Javascript中检测操作系统版本并重定向 [英] Detecting OS version in Javascript and redirecting
本文介绍了在Javascript中检测操作系统版本并重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天
我做了一些研究,发现你可以使用以下javascript来检测用户操作系统,无论是Android,iOS,Windows等:
I have done some research and found that you can use the following javascript to detect a users OS, whether it is Android, iOS, Windows etc:
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);
现在我想做的是根据他的操作系统将用户重新定位到Apple Appstore或google Play商店如下:
Now what I would like to do is to relocate a user, based on his OS, to either the apple Appstore or google Play Store like this:
HTML:
<a href="" id="redirect">Download our App</a>
以及相关联的JS
if (OSName="MacOS" ){
$("#redirect").attr("href", "http://www.itunes.com/myapp")
}
elseif (OSName="Linux"){
$("#redirect").attr("href", "http://www.play.google.com/")
}
(Linux is for Android right? )
是这是正确/最好的方式/我的代码是否有效?
Is this the right/best way of doing it/ Will my code work?
谢谢
推荐答案
您的代码可以简化:
var playStoreUrl = "http://www.play.google.com/",
appStoreUrl = "http://www.itunes.com/myapp",
platform = navigator.platform;
if (/mac/i.test(platform))
$("#redirect").attr("href", appStoreUrl);
else if (/linux/i.test(platform))
$("#redirect").attr("href", playStoreUrl);
else
// Handle the case where the OS is neither MacOS nor Linux
这篇关于在Javascript中检测操作系统版本并重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文