最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to deep-link in websites - Stack Overflow

programmeradmin4浏览0评论
<a class="grey-text text-lighten-3" target="_blank" href="javascript:void(0);" onclick="document.write(yourOS())">Instagram</a>



function yourOS() {
var ua = navigator.platform.toLowerCase();
if (ua.indexOf("android") != -1) {
    document.write(.link("instagram://user?username=owendunnigan"));
} else {
    .link("");
}
}

I am trying to deep link apps into my website, but I can't figure out how to have it go to regular old Instagram on the puter, but then go to the app on Android or iOS device. I know that the links work because I tried them out individually.

<a class="grey-text text-lighten-3" target="_blank" href="javascript:void(0);" onclick="document.write(yourOS())">Instagram</a>



function yourOS() {
var ua = navigator.platform.toLowerCase();
if (ua.indexOf("android") != -1) {
    document.write(.link("instagram://user?username=owendunnigan"));
} else {
    .link("http://www.instagram./OwenDunnigan");
}
}

I am trying to deep link apps into my website, but I can't figure out how to have it go to regular old Instagram on the puter, but then go to the app on Android or iOS device. I know that the links work because I tried them out individually.

Share Improve this question asked Jan 31, 2016 at 21:07 Owen DunniganOwen Dunnigan 351 gold badge1 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I would approach this in a different manner. Instead of onclick checking the source, I would do this onload. So:

    <script>
    function onLoad(){
       var urlLink = "http://www.instagram./OwenDunnigan";
       var urlLink2 = "http://www.twitter./OwenDunnigan";
       var urlLink3 = "http://www.facebook./OwenDunnigan";
       var ua = navigator.userAgent.toLowerCase();
       var isAndroid = ua.indexOf("android") > -1;
       if(isAndroid) {
          urlLink = "instagram://user?username=owendunnigan";
          urlLink2 = "twitter://user?username=owendunnigan";
          urlLink3 = "facebook://user?username=owendunnigan";
       }
       document.getElementById('yourLink').setAttribute('href', urlLink);
       document.getElementById('yourLink2').setAttribute('href', urlLink2);
       document.getElementById('yourLink3').setAttribute('href', urlLink3);
    }
    window.onload = onLoad;
    </script>
    
    <a id="yourLink" class="grey-text text-lighten-3" target="_blank">Link</a>

I tested this on my android device / laptop and it worked for both.

Edit: Down and dirty way. You could make a more OOP style by having a function for the onload function to call passing in the variables, but for simplicity, this does the trick.

Edit2:

<script>
        function getMobileOperatingSystem() {
           var userAgent = navigator.userAgent || navigator.vendor || window.opera;
           if( userAgent.match( /iPad/i ) || userAgent.match( /iPhone/i ) || userAgent.match( /iPod/i ) )
           {
              return 'iOS';
           }
           else if( userAgent.match( /Android/i ) )
           {
              return 'Android';
           }
           else
           {
              return 'unknown';
           }
        }

        function onLoad(){
           var urlLink1 = "http://www.instagram./OwenDunnigan";
           var urlLink2 = "http://www.twitter./OwenDunnigan";
           var urlLink3 = "http://www.facebook./OwenDunnigan";
           switch(getMobileOperatingSystem()){
               case 'Android':
                    urlLink1 = "instagram://user?username=owendunnigan";
                    urlLink2 = "twitter://user?username=owendunnigan";
                    urlLink3 = "facebook://user?username=owendunnigan";
                    break;
               case 'iOS':
                    urlLink1 = "instagram://user?username=owendunnigan";
                    urlLink2 = "twitter://user?username=owendunnigan";
                    urlLink3 = "facebook://user?username=owendunnigan";
                    break;
               default:
                    break;
            }
           document.getElementById('yourLink1').setAttribute('href', urlLink1);
           document.getElementById('yourLink2').setAttribute('href', urlLink2);
           document.getElementById('yourLink3').setAttribute('href', urlLink3);
        }
        window.onload = onLoad;
        </script>
        
        <a id="yourLink1" class="grey-text text-lighten-3" target="_blank">Link1</a>
        <a id="yourLink2" class="grey-text text-lighten-3" target="_blank">Link2</a>
        <a id="yourLink3" class="grey-text text-lighten-3" target="_blank">Link3</a>

Cleaned up the code and rolled in the function from here: Detecting iOS / Android Operating system You can add other systems by simply expanding the detect function and adding case statements. Granted, I didn't add a default case statement which you normally do, but I invoked the variables first, so no reason to reset them in my opinion, but you could do a lot more with it. Let me know if this doesn't work.

The switch from Munsterlander solution could be written like this in order to avoid duplicates:

switch(getMobileOperatingSystem()){
               case 'Android':
               case 'iOS':
                    urlLink1 = "instagram://user?username=owendunnigan";
                    urlLink2 = "twitter://user?username=owendunnigan";
                    urlLink3 = "facebook://user?username=owendunnigan";
                    break;
               default:
                    break;
            }

Sorry for not replying to the solution above but I can't.

you can use branch.io which it way easier to implement deeplink

发布评论

评论列表(0)

  1. 暂无评论