Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kmallea/6784568 to your computer and use it in GitHub Desktop.
Save kmallea/6784568 to your computer and use it in GitHub Desktop.
(function () {
// tries to execute the uri:scheme
function uriSchemeWithHyperlinkFallback(uri, href) {
var start, end, elapsed;
// start a timer
start = new Date().getTime();
// attempt to redirect to the uri:scheme
// the lovely thing about javascript is that it's single threadded.
// if this WORKS, it'll stutter for a split second, causing the timer to be off
document.location = uri;
// end timer
end = new Date().getTime();
elapsed = (end - start);
// if there's no elapsed time, then the scheme didn't fire, and we head to the url.
if (elapsed < 1) {
document.location = href;
}
}
// `intent` is the class we're using to wire this up. Use whatever you like.
$('a.intent').on('click', function (event) {
uriSchemeWithHyperlinkFallback($(this).data('scheme'), $(this).attr('href'));
// we don't want the default browser behavior kicking in and screwing everything up.
event.preventDefault();
});
})();
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="uriSchemeWithHyperlinkFallback.js"></script>
</head>
<body>
<!-- links will work as expected where javascript is disabled-->
<a class="intent"
href="http://facebook.com/someProfile"
data-scheme="fb://profile/10000">facebook</a>
</body>
</html>
@ChaseFlorell
Copy link

Nice improvement. How is it working?

@andrepura
Copy link

i just tested it and it works great using android

@gianpaj
Copy link

gianpaj commented Mar 21, 2016

How do you for example make a facebook share?

@cropots
Copy link

cropots commented Dec 13, 2019

Not work for me on chrome android.

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