simplest cross-browser check if protocol handler is registered

后端 未结 5 1411
青春惊慌失措
青春惊慌失措 2020-12-02 14:47

When user clicks link with custom protocol (like myapp://superlink)

I need either launch an app or allow user to download and run configuration app

5条回答
  •  温柔的废话
    2020-12-02 15:03

    There is this old tricks that it always never fails me.

    The core functionality that you need is setTimeout. I will tell you in detail:

    setTimeout(function() {
      window.location = "http://itunes.com/app/yourapplocation";
    }, 200);
    
    // once you do the custom-uri, it should properly execute the handler, otherwise, the settimeout that you set before will kick in
    window.location = "myapp://superlink";
    

    Now you mentioned that it maybe a link or links so I made this nice function just for your convenience:

    HTML code

    Click here
    

    JS code

    $("a[href*='myapp://']").click(function(e)
    {
      var el = $(this);
      setTimeout(function() {
        window.location = el.data("data-href-alt");
      }, 200);
    
      // once you do the custom-uri, it should properly execute the handler, otherwise, the settimeout that you set before will kick in
      window.location = el.data("href");
    
      e.preventDefault();
    });
    

    Hope this will help you :)

提交回复
热议问题