Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Examples showing how to detect Cordova vs. browser, and how to debug Cordova.
/**!
*
* Utility class for detecting whether or not we're in PhoneGap vs. the browser
* @author nolan
**/
/** set to true if you want to pretend we're PhoneGap in the browser **/
var FORCE_CORDOVA = false;
var CordovaVsBrowser = {
/**
* Execute cordovaFunction if we're in cordova (PhoneGap), else execute browserFunction
*
* e.g. CordovaVsBrowser.execute(function() {alert('hello cordova!');}, function() {alert('hello browser!');});
*
*/
execute : function (cordovaFunction, browserFunction) {
if (FORCE_CORDOVA) {
// pretend we're using phonegap by using StopGap (https://github.com/alunny/stopgap)
StopGap();
}
function onDeviceReady(isBrowser) {
if (!isBrowser || FORCE_CORDOVA) { // cordova
if (cordovaFunction) {
cordovaFunction();
}
} else { // browser
if (browserFunction) {
browserFunction();
}
}
}
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady(true); // this is the browser
}
},
/** return true if we're in Cordova as opposed to the browser **/
isCordova : function() {
return FORCE_CORDOVA || navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/);
}
}
<!-- StopGap code from https://github.com/alunny/stopgap -->
<script src="stopgap.js"></script>
<script src="cordova-vs-browser.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment