Detect between a mobile browser or a PhoneGap application

Diogo Cardoso picture Diogo Cardoso · Apr 27, 2012 · Viewed 38.1k times · Source

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

Answer

EricL picture EricL · Sep 4, 2012

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
}