简体   繁体   中英

Opening Android app using <a> tag in html

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM