Created
July 2, 2014 22:53
-
-
Save robcolburn/d499c973b2ff4e2bc3ac to your computer and use it in GitHub Desktop.
This file contains 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 UA = navigator.userAgent; | |
if (UA.match('Android') && !UA.match('Kindle')) { | |
tryIframeApproach(data.app_link, data.store_link.android); | |
} | |
else if (UA.match('iPhone|iPad|iPod')) { | |
tryIframeApproach(data.app_link, data.store_link.ios); | |
} | |
else { | |
location.href = data.web_link; | |
} | |
/** | |
* The Usual Trick | |
*/ | |
function tryIframeApproach (app_url, fallback_url) { | |
var iframe = document.createElement("iframe"); | |
iframe.style.border = "none"; | |
iframe.style.width = "1px"; | |
iframe.style.height = "1px"; | |
iframe.onload = function () { | |
document.location = fallback_url; | |
}; | |
iframe.onerror = function () { | |
document.location = fallback_url; | |
}; | |
iframe.src = app_url; | |
document.body.appendChild(iframe); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment