Last active
October 18, 2019 21:51
-
-
Save amitsaxena/8601424 to your computer and use it in GitHub Desktop.
CustomURL: Launch app if app is installed, else open an alternate URL (iOS all browsers)
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
<script type="text/javascript"> | |
var timer; | |
var heartbeat; | |
var lastInterval; | |
function clearTimers() { | |
clearTimeout(timer); | |
clearTimeout(heartbeat); | |
} | |
window.addEventListener("pageshow", function(evt){ | |
clearTimers(); | |
}, false); | |
window.addEventListener("pagehide", function(evt){ | |
clearTimers(); | |
}, false); | |
function getTime() { | |
return (new Date()).getTime(); | |
} | |
// For all other browsers except Safari (which do not support pageshow and pagehide properly) | |
function intervalHeartbeat() { | |
var now = getTime(); | |
var diff = now - lastInterval - 200; | |
lastInterval = now; | |
if(diff > 1000) { // don't trigger on small stutters less than 1000ms | |
clearTimers(); | |
} | |
} | |
function launch_app_or_alt_url(el) { | |
lastInterval = getTime(); | |
heartbeat = setInterval(intervalHeartbeat, 200); | |
document.location = 'myapp://customurl'; | |
timer = setTimeout(function () { | |
document.location = 'http://alternate.url.com'; | |
}, 2000); | |
} | |
$(".source_url").click(function(event) { | |
launch_app_or_alt_url($(this)); | |
event.preventDefault(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cweekly Belatedly:
I am implementing this as its own page on my subdomain that is opened for the desired page with a normal
a
link that specifiestarget=_blank
. The timeout then has awindow.close()
in it.This seems to get around the issues with 'invalid address' as the window will be automatically closed - users may still see it for a brief second. Also this isolates the awful ios hackery to one page on the site.