需求:
- 判断是否在微信中打开?是就出现遮罩层,让其在浏览器中打开
- 判断是安卓还是苹果?不同对应的方法不同
判断是否在微信中
是返回true,否返回false
var wx = (function() {
return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1;
})();
判断是苹果还是安卓
返回结果同上
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
打开APP
function android(id) {
var state = null;
try {
state = window.open(android同事给的协议://?后面随便拼参数", '_blank');
} catch(e) {}
if (state) {
window.close();
} else {
window.location = "要跳转的页面URL";
}
}
function ios(id) {
var loadDateTime = new Date();
window.setTimeout(function() {
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime < 5000) {
window.location = "要跳转的页面URL";
} else {
window.close();
}
},
25);
window.location = "ios同事给的协议://?后面随便拼参数";
}
function btnFn(id) {
if (wx) {
$('.zzc').show();
} else {
if (isiOS) {
ios(id)
} else {
android(id)
}
}
};