Getting net::ERR_UNKNOWN_URL_SCHEME error

Swathi Delampady picture Swathi Delampady · Jan 4, 2018 · Viewed 13.6k times · Source

I am trying to open a android application from javascript. If the android application is installed in android mobile, it opens required application. But if android application is not installed, it should give me the popup saying, "You do not seem to have app installed, do you want to download it now?". But I am getting net::ERR_UNKNOWN_URL_SCHEME error. I have added the code snippet below:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function redirectToApp() {
$('#link').attr('href', "<scheme>://<package>/?<parameters>");
$("#link")[0].click();
setTimeout(
        function() {
          if (confirm('You do not seem to have app installed, do you want to download it now?')) {
            window.location = 'https://play.google.com/store/apps/details?id=123';
                      } 
        }, 500);
}
</script>
</head>
<body onload="redirectToApp()">
<a id="link" href="" style="display: none">Link</a>
</body>
</html>

Answer

Blake Simpson picture Blake Simpson · Jan 4, 2018

You could try to wrap the click in a try...catch. You said if the app is installed it works, so then you can assume if it fails that the app is not installed and then run the confirm.

try {
  $('#link').attr('href', "<scheme>://<package>/?<parameters>");
  $("#link")[0].click(); 
} catch (error) {
  if (confirm('You do not seem to have app installed, do you want to download it now?')) {
    window.location = 'https://play.google.com/store/apps/details?id=123';
  } 
}

You may also want to check that the error passed to catch is the net::ERR_UNKNOWN_URL_SCHEME that you expected too but maybe it is enough like this?