Skip to content

Instantly share code, notes, and snippets.

@mvickers
Created September 11, 2012 02:13
Show Gist options
  • Save mvickers/3695436 to your computer and use it in GitHub Desktop.
Save mvickers/3695436 to your computer and use it in GitHub Desktop.
Load Cordova ?
<head>
<script>
var getOS = function { //Just using indexOf() is dirty. Returns iOS/Android/unknown.
var iOS = ['iPad', 'iPod', 'iPhone', 'iPhone Simulator'], //TODO: Remove for release - This is just for debugging.
tmp = new RegExp('\\((.*?)\\)').exec(navigator.userAgent).slice(1).toString().split('; '),
OS, i;
for (i=0; i<tmp.length; i++) {
if (iOS.lastIndexOf(tmp[i]) !== -1) {
OS = 'iOS';
break;
}
else if (tmp[i].substring(0,7) === 'Android') {
OS = 'Android';
break;
}
}
return (typeof OS !== 'undefined') ? OS : 'unknown';
}
var loadJS = function(URL) {
var e = document.createElement("script");
e.src = URL;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}
var loadCordova = function() { //Note: For some reason, we can't (yet) load Google Maps API from here.
var OS = getOS();
if (OS === "Android" || OS === "iOS") {
loadJS('scripts/cordova-' + getOS().toLowerCase + '-2.0.0.js' ); //Platform specific cordova.js file.
}
loadCordova();
}
</script>
<!--
MORE Regular stuff here...
-->
</head>
@zzuhan
Copy link

zzuhan commented Mar 6, 2013

I wanna why using indexOf() is dirty;thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment