日常開發很常見的功能,在移動端須要根據javaScript來判斷手機是安卓仍是ios進行應用市場跳轉。java
經過js提供的Navigator 能夠來判斷手機是安卓仍是ios系統,代碼以下ios
function IsAppleStore() { var u = navigator.userAgent, app = navigator.appVersion; var ios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); var iPad = u.indexOf('iPad') > -1; var iPhone = u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1; if (ios || iPad || iPhone) { return true; } else { return false; } }
function IsAndroidList() { var u = navigator.userAgent; if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) { return true } else { return false } }
在這裏進行判斷跳轉對應的應用市場app
if (IsAppleStore()) {
window.location.href = "ios提供跳轉appstore連接"; //ios
} else {
window.location.href = "安卓提供跳轉的應用市場的連接"; //打開安卓
}
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; }