Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
Last active October 24, 2019 12:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save amitsaxena/9cb0712e572c39a41edc to your computer and use it in GitHub Desktop.
Save amitsaxena/9cb0712e572c39a41edc to your computer and use it in GitHub Desktop.
CustomURL: Launch app if app is installed, else open an alternate URL (Android all browsers)
<script type="text/javascript">
var custom = "myapp://custom_url";
var alt = "http://mywebsite.com/alternate/content";
var g_intent = "intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end";
var timer;
var heartbeat;
var iframe_timer;
function clearTimers() {
clearTimeout(timer);
clearTimeout(heartbeat);
clearTimeout(iframe_timer);
}
function intervalHeartbeat() {
if (document.webkitHidden || document.hidden) {
clearTimers();
}
}
function tryIframeApproach() {
var iframe = document.createElement("iframe");
iframe.style.border = "none";
iframe.style.width = "1px";
iframe.style.height = "1px";
iframe.onload = function () {
document.location = alt;
};
iframe.src = custom;
document.body.appendChild(iframe);
}
function tryWebkitApproach() {
document.location = custom;
timer = setTimeout(function () {
document.location = alt;
}, 2500);
}
function useIntent() {
document.location = g_intent;
}
function launch_app_or_alt_url(el) {
heartbeat = setInterval(intervalHeartbeat, 200);
if (navigator.userAgent.match(/Chrome/)) {
useIntent();
} else if (navigator.userAgent.match(/Firefox/)) {
tryWebkitApproach();
iframe_timer = setTimeout(function () {
tryIframeApproach();
}, 1500);
} else {
tryIframeApproach();
}
}
$(".source_url").click(function (event) {
launch_app_or_alt_url($(this));
event.preventDefault();
});
</script>
@yashwanthkumar1796
Copy link

Hi, this isn't working in ios 9 > in safari

@amitsaxena
Copy link
Author

@yashwanthkumar1796 it has been ages since I dug deep in this code and created the snippet, but if I remember correctly, the iOS version can be found here: https://gist.github.com/amitsaxena/8625016

Also these 2 blog posts I wrote then might have more context:
https://aawaara.com/post/74543339755/smallest-piece-of-code-thats-going-to-change-the
https://aawaara.com/post/88310470252/smallest-piece-of-code-thats-going-to-change-the

@yashwanthkumar1796
Copy link

@yashwanthkumar1796 it has been ages since I dug deep in this code and created the snippet, but if I remember correctly, the iOS version can be found here: https://gist.github.com/amitsaxena/8625016

Also these 2 blog posts I wrote then might have more context:
https://aawaara.com/post/74543339755/smallest-piece-of-code-thats-going-to-change-the
https://aawaara.com/post/88310470252/smallest-piece-of-code-thats-going-to-change-the

@amitsaxena do you have any solution for ios 9 or greater

@amitsaxena
Copy link
Author

Did you try the ones mentioned in blog post (for iOS) already?

@yashwanthkumar1796
Copy link

yashwanthkumar1796 commented Oct 24, 2019

Did you try the ones mentioned in blog post (for iOS) already?

Yes i did, i found a solution in chrome using iframe but i couldn't solve in safari

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