Last active
December 12, 2015 03:18
-
-
Save nolanlawson/4705951 to your computer and use it in GitHub Desktop.
Examples showing how to detect Cordova vs. browser, and how to debug Cordova.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**! | |
* | |
* 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)/); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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