JS to determine whether the mobile side installs an app's many ways

Source: Internet
Author: User

This article explains the JS to determine whether the mobile terminal installation of an application of a variety of methods, share to everyone for reference, the specific content as follows

The first method:

One: Judging is the kind of device

?  A var isandroid = U.indexof ('Android') >-1 | | U.indexof ('Linux') >-1/ ///

This article explains the JS to determine whether the mobile terminal installation of an application of a variety of methods, share to everyone for reference, the specific content as follows

The first method:

One: Judging is the kind of device

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

Two: Android device: principle: Judge whether to know this agreement, know the direct jump, do not know to download the app here

?
123456789 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);       };

Two: iOS device: principle: Judge whether to know this agreement, know directly jump, do not know here download appios ();

?
123456789101112 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)       }; }

The second method:

Although it is possible to start an app in JS, it is not possible to determine if the app is installed;
Start the app takes a long time, JS interrupt time is long, if not installed, JS instantaneous execution completed. Go directly to the code!
HTML code:

?
1 <ahref="javascript:testApp(‘tel:1868888888‘)">打电话</a>

JS Code:

?
1234567891011121314151617181920212223 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);  }

The third method:

Recently in the WAP version of the project, there is a need to determine whether the mobile phone has our app, if some words open the application, not only jump to the WAP page.
WAP is simply a website that runs on a mobile browser. Regardless of where the application is, in short, the browser chant, you can use JS to determine whether there is an application, the implementation of the actual way is to convert the HTTP protocol to the local software protocol.
Or just stick to the code.
As follows:

?
12345678910111213141516171819202122232425 <script language="javascript">  if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/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>

What is the apps custom URL schemes?
is actually a protocol URL that you agree with the app, and your iOS colleague or Android colleague sets a URL Scheme when writing a program.
For example, set:
URL Scheme:app
The application can then be called by other programs via URLString = app://.
You can also pass parameters, such as:
App://reaction/?uid=1
principle:within 500ms, this machine has the application to resolve this protocol and open the program, call the application, if the native no application can resolve the protocol, or 500MS does not open the program, then execute settimeout inside the function, is to jump to the page you want to jump to.

The above is the JS to determine whether the mobile terminal installation of a variety of methods, I hope that everyone's learning has helped.

JS to determine whether the mobile side installs an app's many ways

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.