0

Using <a> tag and window.location(), how to open the android app when the link is clicked else use the browser.

For example, i have the facebook link: fb://profile/10001901208 and https://www.facebook.com/profile.php?id=10001901208.I tried onclick() method but it seems to work differently.

<script>
window.onload = function() {
  if(navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
    window.location = "<TWITTER MOBILE DEEPLINK>"; 
    setTimeout(function(){
        window.location = "<LINK TO TWITTER ONLINE>";
    }, 50);   
  } else {
    window.location = "<LINK TO TWITTER ONLINE>";
  }
}
</script>

I tried @media screen and hide and show the tag but it didnt work as expected. Since it is predetermined, if the app is not installed it says; Page couldnt load.

<a href="fb://profile/10001901208" target="_blank" class="mobile"><li class="fa fa-facebook"></li></a>

<a href="https://www.facebook.com/profile.php?id=10001901208" target="_blank" class="web"><li class="fa fa-facebook"></li></a>

I expect the output of opening the app if installed else open in the browser using window.location() and if possible with <a> tag.

1 Answers1

0

You need to find out if the user is using a cell phone, or a computer.

This is how I do it.

<script>
    function getMobileOperatingSystem() {
        var userAgent = navigator.userAgent || navigator.vendor || window.opera;
        var putLinksInDiv = document.getElementById('putLinksInDiv');
        //Windows Phone or Android or iOS
        if (/windows phone/i.test(userAgent) || /android/i.test(userAgent) || (/iPad|iPhone|iPod/.test(userAgent) &&!window.MSStream)) {
            putLinksInDiv.innerHTML = "<a href=\"fb://profile/123\">link</a>";
        } else {
           putLinksInDiv.innerHTML = "<a href=\"http://www.facebook.com/123\">link</a>";
        }
    }
</script>

In your body do this.

<body onload="getMobileOperatingSystem();">

And then in your HTML, add a div for the links.

<div id="putLinksInDiv"></div>
letsCode
  • 2,774
  • 1
  • 13
  • 37
  • too bad if the app isnt installed then it will say Page couldnt load – Jose Harrison Batoctoy Jan 14 '19 at 15:35
  • but thank you man, it really helped me. Like, it gives me an idea what the syntax looks like. Thanks! – Jose Harrison Batoctoy Jan 14 '19 at 15:36
  • @JoseHarrisonBatoctoy the easiest way to find the ID of a profile is to right click on their profile image. click on copy link address. Paste this URL somewhere. you will notice that the URL has 3 params (fbid/id/set). For the profile you are using, use the id param. i tested, it works. – letsCode Jan 14 '19 at 15:38