Examples of JavaScript interacting with Android and iOS

Source: Internet
Author: User


First, for Android and iOS we have to do a different operation, the following is to determine the system's JS code


Alert (IsMobile (1)); 1 indicates that android,0 iOS
function IsMobile (test) {
var u = navigator.useragent, app = Navigator.appversion;
if (/applewebkit.*mobile/i.test (navigator.useragent) | | (/midp| symbianos| nokia| Samsung| Lg| nec| tcl| alcatel| bird| dbtel| dopod| philips| haier| lenovo| mot-| nokia| sonyericsson| sie-| amoi| Zte/.test (navigator.useragent))) {
if (Window.location.href.indexOf ("? mobile") <0) {
try{
if (/iphone|mac|ipod|ipad/i.test (navigator.useragent)) {
Return ' 0 ';
}else{
Return ' 1 ';
}
}catch (e) {}
}
}else if (u.indexof (' IPad ') >-1) {
Return ' 0 ';
}else{
Return ' 1 ';
}
}

Second, JS call app End Function, the following is JS code


if (IsMobile (1) ==1) {
Android.settypeactivity (ID,TYPE,HREF);
}else{
Jstoios (ID,TYPE,HREF);
}

Settypeactivity is a function defined on the Android side, Jstoios is a function defined on the iOS side, and is a parameter that is passed by the JS side in parentheses.

Third, as for the JS end of how to obtain the app end of the data, just in the JS end to provide the app End Function name, in the Notification app end call can

There is also a way to pass the data to another page without using the app, which is through the URL

In this page, the data is spliced behind the IP

On another page, get the parameters after the URL by using the following method


function Getrequest () {
var url = location.search; Get the "?" in the URL String after the character
var therequest = new Object ();
if (Url.indexof ("?")!=-1) {
var str = URL.SUBSTR (1);
STRs = Str.split ("&");
for (var i = 0; i < strs.length i + +) {
Therequest[strs[i].split ("=") [0]]=decodeuricomponent (Strs[i].split ("=") [1]);
}
}
return therequest;
}
Get URL parameters
var Request = new Object ();
Request = Getrequest ();
var name;
name = request[' name '];
Just started to do with the app interactive HTML5, a bit overwhelmed, in fact, this description on the internet has a lot of information, I am now specialized in front of the point of view to sum up.

Many native apps now embed more or less HTML5 pages, so it's important to know how JS interacts with native apps, and how to interact better to optimize app interaction, which requires constant practice.

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.