直接上代碼
var APPCommon = {
downAppURl : "http://**/",//下載APP地址
downWeixin: "http://**",//微信下載APP地址
iphoneSchema: 'openApp.jdMobile://',//IOS打開APP 跳轉地址(這個跳轉地址需要IOS開發人員提供)
iphoneDownUrl: 'https://**',//IOS 下載地址
androidSchema: 'openApp.jdMobile://',//安卓打開APP 跳轉地址 (這個跳轉地址需要安卓開發人員提供)
androidDownUrl: 'http://**/',//安卓下載地址
openApp: function(){
var this_ = this;
if(this_.isWeixin()){ //微信
window.location = this_.downWeixin;
}
}else{//非微信瀏覽器
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
var loadDateTime = new Date();
window.setTimeout(function() {
var timeOutDateTime = new Date();
if (loadDateTime && timeOutDateTime - loadDateTime < (1500 + 200)) {
window.location = this_.iphoneDownUrl;//ios下載地址
} else {
window.close();
}
},1600); //此處時間有待確定
window.location = this_.iphoneSchema;
}else if (navigator.userAgent.match(/android/i)) {
var loadDateTime = new Date();
var ifrSrc =this_.androidSchema;
if (!ifrSrc) {
return;
}
var ifr = document.createElement('iframe');
ifr.src = ifrSrc;
ifr.style.display = 'none';
document.body.appendChild(ifr);
var TimeLoad = setTimeout(function() {
document.body.removeChild(ifr);
window.location=this_.androidDownUrl; //android下載地址
}, 1600);
window.onblur=function(){
clearTimeout(TimeLoad);
};
}else{ //非ios、非安卓
window.location = this_.downAppURl;
}
}
},
isWeixin: function(){ //判斷是否是微信
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
return true;
} else {
return false;
}
}
};
function openAP(){
APPCommon.openApp()
}
備:
1、經過測試ios用iframe嘗試打開APP是行不通的,於是換了一個思路,用響應時間來控制。
2、ios安裝APP自帶瀏覽器safari會有alert框,目前未找到解決辦法屏蔽。
3、微信禁止了打開APP 這個功能,所以建議跳轉地址為到應用寶。