Home > Web Front-end > JS Tutorial > js multiple methods to determine whether a certain app is installed on the mobile terminal_javascript skills

js multiple methods to determine whether a certain app is installed on the mobile terminal_javascript skills

WBOY
Release: 2016-05-16 15:25:02
Original
1351 people have browsed it

The examples in this article explain the various methods of using js to determine whether a certain app is installed on the mobile terminal, and share it with everyone for your reference. The specific content is as follows

First method:

1: Determine what kind of equipment it is

var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

Copy after login

Two: Android devices: Principle: Determine whether you know the protocol. If you know it, jump directly. If you don’t know it, download the app here

android();

if(isAndroid){
      function android(){
        window.location.href = "openwjtr://com.tyrbl.wjtr"; /***打开app的协议,有安卓同事提供***/
        window.setTimeout(function(){
           window.location.href = "http://www.wjtr.com/download/index.html"; /***打开app的协议,有安卓同事提供***/
        },2000);
      };

Copy after login

2: ios device: Principle: Determine whether you know this protocol. If you know it, jump directly. If you don’t know it, download appios();

if(isiOS){
      function ios(){
        var ifr = document.createElement("iframe");
        ifr.src = "openwjtr://com.tyrbl.wjtr"; /***打开app的协议,有ios同事提供***/
        ifr.style.display = "none"; 
        document.body.appendChild(ifr);
        window.setTimeout(function(){
          document.body.removeChild(ifr);
           window.location.href = "http://www.wjtr.com/download/index.html"; /***下载app的地址***/
        },2000)
      };
}
Copy after login

Second method:

Although you can start an app in JS, it cannot determine whether the app is installed;
It takes a long time to start the app, and the js interruption time is long. If it is not installed, the js will be executed instantly. Just jump into the code!
html code:

<a href="javascript:testApp('tel:1868888888')">打电话</a> 
Copy after login

js code:

function testApp(url) { 
  var timeout, t = 1000, hasApp = true; 
  setTimeout(function () { 
    if (hasApp) { 
      alert('安装了app'); 
    } else { 
      alert('未安装app'); 
    } 
    document.body.removeChild(ifr); 
  }, 2000) 
 
  var t1 = Date.now(); 
  var ifr = document.createElement("iframe"); 
  ifr.setAttribute('src', url); 
  ifr.setAttribute('style', 'display:none'); 
  document.body.appendChild(ifr); 
  timeout = setTimeout(function () { 
     var t2 = Date.now(); 
     if (!t1 || t2 - t1 < t + 100) { 
       hasApp = false; 
     } 
  }, t); 
} 
Copy after login

Third method:

I am working on the wap version of the project recently. One of the requirements is to first determine whether our APP application is available on the mobile phone. If so, open the application. If not, jump to the wap page.
Simply put, a wap is a website that runs on a mobile browser. No matter where the application is, it is just a browser. You can use JS to determine whether there is an application locally. The actual implementation is to convert the http protocol into a local software protocol.
Just post the code directly.
As follows:

<script language="javascript">
 if (navigator.userAgent.match(/(iPhone|iPod|iPad);&#63;/i)) {
  var loadDateTime = new Date();
  window.setTimeout(function() {
   var timeOutDateTime = new Date();
   if (timeOutDateTime - loadDateTime < 5000) {
    window.location = "要跳转的页面URL";
   } else {
    window.close();
   }
  },
  25);
  window.location = " apps custom url schemes ";
 } else if (navigator.userAgent.match(/android/i)) {
  var state = null;
  try {
   state = window.open("apps custom url schemes ", '_blank');
  } catch(e) {}
  if (state) {
   window.close();
  } else {
   window.location = "要跳转的页面URL";
  }
 }
</script>
Copy after login

What are apps custom url schemes?
is actually a protocol URL agreed between you and the APP. Your IOS or Android colleagues will set up a URL Scheme when writing the program,
For example setting:
URL Scheme: app
Then other programs can call the application through URLString = app://.
You can also pass parameters, such as:
app://reaction/?uid=1
Principle: Within 500ms, there is an application on this machine that can parse this protocol and open the program and call the application; if there is no application on this machine that can parse this protocol or this machine does not open this within 500ms program, execute the function in setTimeout, which is to jump to the page you want to jump to.

The above are various methods for JS to determine whether a certain app is installed on the mobile terminal. I hope it will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template