0

I have a html page, I want to launch my native app when loading this page. the following is my code:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-1.12.1.js"></script>
</head>
<body>
<br><br><br>
<a id="link1" style='display:none' href="baidufe://schemedemo/get/info?id=10000">test link</a>
<br>
<button onclick="launch()">Launch</button>
</body>
<script language="JavaScript">
    function launch(){
        console.log("test");
        var clickTarget = document.getElementById("link1");
        clickTarget.click();

    };
    window.onload=function(){
        launch();
    };
    </script>
</html>

But when i open this page in android chrome(version is 49), my native app isn't launched. when i click the "Launch" button, my native app can be successfully launched.

what's root cause? but my requirement is native app will be launched when opening this page, how to fix it? Thank!

Steve
  • 281
  • 1
  • 3
  • 14

1 Answers1

0

That seems like a rather indirect way to accomplish that you want...instead of simulating a click event, why not set the window location directly and skip a step?

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
<script>
  setTimeout(function() {
    window.location = 'baidufe://schemedemo/get/info?id=10000'
  }, 250);
</script>
</html>

Keep in mind automatically redirecting users this way has a ton of edge cases, and may show a nasty 'Page could not be found' error if they don't have your app installed.

Alex Bauer
  • 13,147
  • 1
  • 27
  • 44
  • Thanks Alex, but your code don't work. http://blog.tapstream.com/why-has-google-broken-deeplinking-on-android/#comment-1906522285."If a user indicates via a gesture such as a click on a link, or iirc a navigation from a link click that subsequently 302's redirects or window.location redirects within 100 milliseconds of the gesture to an intent: URI then it should all still work". how to simulate a user gesture? – Steve Mar 19 '16 at 03:14