简体   繁体   中英

Getting net::ERR_UNKNOWN_URL_SCHEME error

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>

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM