Skip to content

Instantly share code, notes, and snippets.

@mlzxy
Last active October 20, 2016 19:27
Show Gist options
  • Save mlzxy/5cf9dc3596bf96bb2eeb to your computer and use it in GitHub Desktop.
Save mlzxy/5cf9dc3596bf96bb2eeb to your computer and use it in GitHub Desktop.
Detect platform and redirect for app downloading or app custom schema url
(function() {
var TIMEOUT = 300;
var platform = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
Mobile: function() {
return (platform.Android() || platform.BlackBerry() || platform.iOS() || platform.Opera() || platform.Windows());
},
Desktop: function() {
return !platform.Mobile();
}
};
var paths = window.location.pathname.split('/');
var schoolId = paths[paths.length - 1] || paths[paths.length - 2];
var link = {
iosInstall: 'https://itunes.apple.com/cn/app/qq-kong-jian/id364183992?mt=8',
androidInstall: 'http://kidyuan.com',
appCustomUri: 'kidyuan://joinSchool/' + schoolId,
desktop: 'http://kidyuan.com'
};
if (platform.Desktop()) {
window.location.href = link.desktop;
} else {
window.location.href = link.appCustomUri;
}
// App Not Install
setTimeout(function() {
if (platform.iOS()) {
window.location.href = link.iosInstall;
}
if (platform.Android()) {
window.location.href = link.androidInstall;
}
}, TIMEOUT);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment