Search code examples
javascriptandroidreact-nativewebdeep-linking

Open non http url through script tag


I have the following set of code

<body>
  <script>
    function open() {
      var link = document.getElementById("myid").href
      windows.location.href = link;
    }

    document.addEventListener("DOMContentLoaded", function() { 
      open();
    });
  </script>
  <a id="myid" href=<%="myapp://myapp/"%>>Click here</a>
</body>

The expected behavior was 'myapp' app should open automatically when the page is loaded. But the user has to manually click on "Click here" to open the app.

I tried following set of code as well but did not helped,

<body>
  <script>
    function open() {
      var link = document.getElementById("myid").href
      window.open(link)
    }

    document.addEventListener("DOMContentLoaded", function() { 
      open();
    });
  </script>
  <a id="myid" href=<%="myapp://myapp/"%>>Click here</a>
</body>

I think browsers have some kind of security measures to prevent non HTTP navigation through the script.

NOTE:

  1. I don't have an adblocker neither pop-up block enabled.

Solution

  • you have a typo in windows.location.href, it should be window

    i've tested your code and the redirect works without the typo, however redirecting like that might be blocked by the browser in several cases.