The most effective and reliable method to determine if an application is running on your phone via JavaScript

What is the most effective and reliable method of detecting an application in a telephone conversation or just in a browser for mobile / desktop computers with JavaScript? I am trying to fix any problems that prevent me from testing / debugging my telephone applications in any browser (desktop or mobile) and creating a truly universal code base for my applications.

I intend to structure my functions using special phonebook calls:

if (phonegapisrunning) {
    // phonegap specific javascript calls here
}
else {
    // standard javascript calls here
}

When searching for a solution, I ran into this problem: PhoneGap: detection on startup in a desktop browser .

Although this thread discusses this issue, I do not see a clear answer to the question of which method is the most efficient / reliable. Should I bind to the onDeviceReady () event? Should I check window.device? Is there a more efficient or reliable way to check if the application is running on the phone through JavsScript?

And this thread that mentions the Ripple Chrome plugin: Phonegap web app in regular desktop browsers

Ripple tools look like this can be a valuable testing tool. But I'm trying to make my phone call applications run in a desktop browser without a plugin.

, , useragent sniffing, , , , .

+5
4

, / javascript onDeviceReady Phonegap.

, , , Cordova/Phonegap , .

var string = device.cordova; // or device.phonegap

if (string == null) {
  //do non phonegappy stuff here
} else {
  //do phonegappy stuff
}
+2

. , , - cordova -. javascript android cordova, , ​​ :

window._cordovaNative

javascript ios cordova, :

window._nativeReady

, java- cordova .. - cordova, :

alert("Android: " + window._cordovaNative);
alert("iOS: " + window._nativeReady);

, , - , !

+2

, / deviceready:

var isCordovaReady = true;

:

if (isCordovaReady) {
    // do cordova/phonegap stuff
}
else {
    // do non cordova/phonegap stuff here
}
+1

: PhoneGap: ,

, :

if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
    document.addEventListener("deviceready", onDeviceReady, false); //phone
} else {
    onDeviceReady(); //this is the browser
}

You can change it a bit to work on such projects:

var phonegapisrunning = false;
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
    document.addEventListener("deviceready", onDeviceReady, false); //phone
    //change to true
    phonegapisrunning = true;

} else {
    onDeviceReady(); //this is the browser
}

Hope this helps!

0
source

All Articles