James Wong
James Wong

Reputation: 4619

Detect if running inside PhoneGap/ Cordova

There has been a few questions how to detect an HTML page is running inside a device as a phonegap application.

Most of the solutions (here and here) have been

  1. Detect if browser agent is iPhone/iPad/Android/Blackberry
  2. Use 'deviceready' event to detect if running inside a device.
  3. Use URL to detect whether protocol is HTTP or FILE

Are there any other solutions? All 3 won't work for me since I run the development HTML files locally before compiling the app, as a result it will detect 1). and 2). will fire and 3). is indeed FILE protocol.

Upvotes: 5

Views: 3369

Answers (2)

SyntaxGoonoo
SyntaxGoonoo

Reputation: 1070

This works:

if (window.cordova)
{
    document.addEventListener("deviceready", onReady, false);
}
else 
{
    onReady();
}

Upvotes: 0

Nishanth Nair
Nishanth Nair

Reputation: 2965

In local file and mobile website deviceready won't fire as it is a PhoneGap specific event. so u can set your IsRunningOnPhoneGap flag in the device ready event.

Upvotes: 2

Related Questions