判断浏览器及设备的打开方式,自动跳转app中

 

如果安装了APP则自动条状app,如果没安装则自动跳转下载页面

<head>

放在head中加载

<script>
function redirect() {
var appUri;
var appStoreUrl;
var eventId = getQueryString("eventId");
switch (getDevice()) {
case "iOS": appUri = "yomovie-thing://?eventId=" + eventId; appStoreUrl = "http://www.baidu.com/thing"; break;
case "Android": appUri = "yomovie-thing://?eventId=" + eventId; appStoreUrl = "http://cn.bing.com"; break;
//default: appUri = "#"; appStoreUrl = "";
}
if (appUri != undefined) {
$('<iframe />')
.attr('src', appUri)
.attr('style', 'display:none;')
.appendTo('body');
setTimeout(function () {
document.location.href = appStoreUrl
}, 25);
}
}

function getDevice() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;

if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
return 'iOS';
}
else if (userAgent.match(/Android/i)) {

return 'Android';
}
else {
return 'unknown';
}
}

function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

var browser = {
versions: function () {
var u = navigator.userAgent, app = navigator.appVersion;
return { //移动终端浏览器版本信息
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
};
}(),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
}
</script>
</head>

 

<body>

放在body中加载
<script>
(function () {
if (browser.versions.mobile) {//判断是否是移动设备打开

var ua = navigator.userAgent.toLowerCase();//获取判断用的对象

if (ua.match(/WeiBo/i) == "weibo") {

//在新浪微博客户端打开
}
else if (ua.match(/QQ/i) == "qq") {

//在QQ\微信打开
}
else if (ua.match(/MicroMessenger/i) == "micromessenger") {
//在微信中打开
}
else {
redirect();
}
}
})()
</script>

</body>

你可能感兴趣的:(浏览器)