Skip to content

Instantly share code, notes, and snippets.

@hleinone
Last active August 29, 2015 14:07
Show Gist options
  • Save hleinone/c2796dcb6ffc7e55b0d9 to your computer and use it in GitHub Desktop.
Save hleinone/c2796dcb6ffc7e55b0d9 to your computer and use it in GitHub Desktop.
Redirector
var redirector = new Object();
redirector.openLink = function(url, playStoreId, appStoreId) {
var isAndroid = navigator.userAgent.match(/Android/),
isIos = navigator.userAgent.match(/iPhone|iPad|iPod/),
isFirefox = navigator.userAgent.match(/Firefox/),
isOpera = navigator.userAgent.match(/OPR/);
if (isAndroid) {
if (isFirefox) {
setTimeout('market://details?id=' + playStoreId);
window.location.replace(url);
} else {
var slices = url.match('^([a-z]+)://(.+)$'),
path,
protocol;
if (!slices) {
path = slices[2];
protocol = slices[1];
} else {
window.location.replace(url);
return;
}
if (isOpera) {
setTimeout('market://details?id=' + playStoreId);
}
window.location.replace('intent://' + path + '#Intent;package=' + playStoreId + ';scheme=' + protocol + ';end;');
}
} else if (isIos) {
setTimeout('itms-apps://itunes.apple.com/app/my-app/id' + appStoreId + '&mt=8');
window.location.replace(url);
} else {
window.location.replace(url);
}
function setTimeout(redirectTo) {
var now = new Date().valueOf();
window.setTimeout(function() {
if (new Date().valueOf() - now > 100) return;
if (!isHidden()) {
window.location.replace(redirectTo);
}
}, 50);
}
function isHidden() {
return (typeof document.hidden !== 'undefined' && document.hidden) ||
(typeof document.mozHidden !== 'undefined' && document.mozHidden) ||
(typeof document.webkitHidden !== 'undefined' && document.webkitHidden);
}
};
@hleinone
Copy link
Author

Supports

Android:

  • Chrome
  • Android Browser
  • Samsung Browser
  • HTC Browser
  • Firefox
  • Opera
  • Dolphin

iOS:

  • Safari
  • Chrome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment