Last active
August 29, 2015 14:07
-
-
Save hleinone/c2796dcb6ffc7e55b0d9 to your computer and use it in GitHub Desktop.
Redirector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Supports
Android:
iOS: