Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect between a mobile browser or a PhoneGap application

Is it possible to detect if the user is accessing through the browser or application using JavaScript?

I'm developing a hybrid application to several mobile OS through a web page and a PhoneGap application and the goal would be to:

  1. Use the same code independently of the deployment target
  2. Add PhoneGap.js file only when the user agent is an application
like image 364
Diogo Cardoso Avatar asked Apr 27 '12 08:04

Diogo Cardoso


2 Answers

You could check if the current URL contains http protocol.

var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1; if ( app ) {     // PhoneGap application } else {     // Web page } 
like image 177
EricL Avatar answered Oct 05 '22 23:10

EricL


Quick solution comes to mind is,

onDeviceReady 

shall help you. As this JS call is invoked only by the Native bridge (objC or Java), the safari mobile browser will fail to detect this. So your on device app(phone gap) source base will initiate from onDeviceReady.

And if any of the Phonegap's JS calls like Device.platform or Device.name is NaN or null then its obviously a mobile web call.

Please check and let me know the results.

like image 25
Futur Avatar answered Oct 06 '22 01:10

Futur