JS pull or download app implementation code

Source: Internet
Author: User
Recently, my project encountered such a requirement. I used the mobile phone webpage to determine whether my company app was installed. If my company app was installed, I pulled up the app. If it was not installed, I jumped to the download page. How can this problem be achieved? The following small series will share with you the implementation code for pulling up or downloading the app by js. If you need a friend, refer to the product to raise a requirement and determine whether your company's app is installed on the mobile phone webpage, if the app is installed, the app is pulled up. If the app is not installed, the app is redirected to the download page.

After reading various materials, I tried to summarize a fairly feasible method.

The principle of app pulling is that js and native have a unified address, for example (qiyimobile: // self/qiyi. madeindexpage ). Then the browser initiates a request. If the app is installed, the request is blocked and opened.

The following is the android code configuration. In fact, scheme and host should be consistent with js.

                   
           
           
         
 

Js is a little troublesome. If it is relatively simple to pull the app only, you can directly use a connection or window. location. href =. However, there is a problem here, that is, some mobile phones will identify the Protocol we set to attempt to open this webpage when the app is not installed. Of course, the website cannot be found. Iframe is more compatible.

 var ifr = document.createElement('iframe'); ifr.src = 'qiyimobile://self/qiyi.madeindexpage'; document.body.appendChild(ifr);

When the app is not installed on the mobile phone, you should jump to the download page. Many people say that you can jump directly after a delay, as shown below:

Window. location. href = "open address" setTimeout (function () {window. location. href = ""}, 500 );

Of course, this is also a problem, because some mobile phones do not automatically clear the timer when pulling the app, that is, whether or not the app is installed, the page will jump to the download page after Ms. Therefore, it is the most troublesome thing to determine whether to jump to the download page. The following is a good solution.

Set an initial time. There is an end time after the app address is opened and pulled up. The difference between the two is calculated. If the difference is greater than a certain time, the app is installed. If the difference is very small, the app is not opened, you need to go to the download page.

The final code is as follows:

Downloadapp. addEventListener ('click', function () {var start = new Date (); // records the initial time var t = 500; var ifr = document. createElement ('iframe'); ifr. src = 'qiyimobile: // self/qiyi. madeindexpage '; // open the app document. body. appendChild (ifr); ifr. onload = function () {}; ifr. style. display = 'none'; setTimeout (function () {document. body. removeChild (ifr); var end = new Date (); // record end time console. log (end-start) if (end-start <= (t + 30) {// when the difference between the two is less than 30 ms, jump to the download page window. location. href = "https://www.baidu.com" }}, t );})

The above section describes how to pull or download the implementation code of the app in Javascript. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!

For more articles about how to pull up or download the app implementation code in JS, please follow the PHP Chinese network!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.