js判断手机端操作系统的两种方法

时间:2021-09-16 18:52:29

//判断手机端操作系统(Andorid/IOS),并自动跳转相应下载界面

androidURL ="http://xxx/xxx.apk";

  var browser = {
versions: function() {
var u = navigator.userAgent,
app = navigator.appVersion;
return {
android: u.indexOf("Android") > -1 || u.indexOf("Linux") >-1,
iPhone: u.indexOf("iPhone") > -1 ,
iPad: u.indexOf("iPad") >-1,
iPod: u.indexOf("iPod") > -1,
};
} (),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
} if (browser.versions.iPhone||browser.versions.iPad||browser.versions.iPod)
{
//如果是ios系統,直接跳轉至appstore該應用首頁,傳遞参數为該應用在appstroe的id號
window.location.href="itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=123456";
}
else if(browser.versions.android)
{
window.location.href = androidURL;
} /* //第二种方法
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
//alert(navigator.userAgent);
window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) {
//alert(navigator.userAgent);
window.location.href ="Android.html";
} else {
window.location.href ="pc.html";
}; */