skip to Main Content

How to open a mobile or desktop app with JavaScript from the browser?

A perfect example of what I want to achieve is what Whatsapp and Telegram do when they manage to run their mobile or desktop apps after a click on a website button.

  • Whatsapp (A click on Continue to Chat will open the WhatsApp app);
    enter image description here

  • Telegram (A click on VIEW IN TELEGRAM will open the Telegram app);
    enter image description here

2

Answers


  1. Use Windows.open(app://,"_blank","location=yes)

    Login or Signup to reply.
  2. Example:

    <a href="tg://resolve?domain=my_group_username">Open Telegram group</a>
    <script>
      document.querySelector("a").addEventListener("click", function(event) {
        if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g)) {
          event.preventDefault();
          location.href = this.href;
        }
      });
    </script>
    

    This code creates a link with the URL tg://resolve?domain=my_group_username, which is the URL scheme associated with the Telegram app. When the link is clicked, the Telegram app will be opened and the user will be taken to the specified group.

    Note that this code is specific to the Telegram app and will not work for other apps. Each app has its own custom URL scheme, and you will need to consult the app’s documentation to determine what it is and how to use it.

    eg: Open the url such as https://t.me/publictestgroup , then open the devtools, check the Elements, click the 「Select an element in the page to inspect it」, choose View in Telegram, and you can see the flow:

    <div class="tgme_page_action">
      <a class="tgme_action_button_new shine" href="tg://resolve?domain=publictestgroup">View in Telegram</a>
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search