I use this code:
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady(); //this is the browser
}
UPDATE
There are many other ways to detect if phonegap is running on a browser or not, here is another great option:
var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
if ( app ) {
// PhoneGap application
} else {
// Web page
}
as seen here: Detect between a mobile browser or a PhoneGap application
I wrote a post about it a few days ago. This is the best solution you can find (until PhoneGap will release something, maybe or maybe not), it's short, simple and perfect (I've checked it in every possible way and platform).
This function will do the job for 98% of the cases.
/**
* Determine whether the file loaded from PhoneGap or not
*/
function isPhoneGap() {
return (window.cordova || window.PhoneGap || window.phonegap)
&& /^file:\/{3}[^\/]/i.test(window.location.href)
&& /ios|iphone|ipod|ipad|android/i.test(navigator.userAgent);
}
if ( isPhoneGap() ) {
alert("Running on PhoneGap!");
} else {
alert("Not running on PhoneGap!");
}
To complete the other 2% of the cases, follow these steps (it involves a slight change on native code):
Create a file called __phonegap_index.html, with the source:
<!-- __phonegap_index.html -->
<script type="text/javascript">
function isPhoneGap() {
//the function's content is as described above
}
//ensure the 98% that this file is called from PhoneGap.
//in case somebody accessed this file directly from the browser.
if ( isPhoneGap() )
localStorage.setItem("isPhoneGap","1");
//and redirect to the main site file.
window.location = "index.html";
</script>
Now, on native simply change the start page from index.html to __phonegap_index.html on all your PhoneGap platforms. Let's say my project name is example, the files you need to change are (as for PhoneGap version 2.2.0):
CordovaLibApp/AppDelegate.m
src/org/apache/cordova/example/cordovaExample.java
example/package.appxmanifest
www/config.xml
framework/appinfo.json
src/WebForm.cpp
(line 56)Finally, you can use it anywhere on your site, if it's running on PhoneGap or not:
if ( localStorage.getItem("isPhoneGap") ) {
alert("Running on PhoneGap!");
} else {
alert("Not running on PhoneGap!");
}
Hope it helps. :-)
I know it's been answered a while ago but "PhoneGap.available" doesn't exist anymore. You should use:
if (window.PhoneGap) {
//do stuff
}
or since 1.7, prefer:
if (window.cordova) {
//do stuff
}
EDIT 2019: as said in the comments, this only works if you do not include cordova lib into your desktop browser build. And of course it is a good practice to include only the strict minimum javascript/html/css files for each device you target
The most trustable way we found to tell if we are in a cordova/phonegap application is to modify the cordova application's user agent using this config AppendUserAgent.
In config.xml
add:
<preference name="AppendUserAgent" value="Cordova" />
Then call:
var isCordova = navigator.userAgent.match(/Cordova/i))
Why?
window.cordova
and document.addEventListener('deviceready', function(){});
are subject to racing conditions navigator.standalone
does not work when <content src="index.html" />
is a website (Ex: <content src="https://www.example.com/index.html" />
or with cordova-plugin-remote-injection)I think this is simplest:
var isPhoneGap = (location.protocol == "file:")
EDIT For some people that didn't work. Then you might try (haven't tested)
var isPhoneGap = ! /^http/.test(location.protocol);
This works for me (running 1.7.0)
if (window.device) {
// Running on PhoneGap
}
Tested on desktop Chrome and Safari.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With