Skip to content

Instantly share code, notes, and snippets.

@darkseed
Created February 11, 2014 16:02
Show Gist options
  • Save darkseed/8937785 to your computer and use it in GitHub Desktop.
Save darkseed/8937785 to your computer and use it in GitHub Desktop.
Javascript Function to determine if where running as a webapp
function detectWebApp() {
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") ]]> -1;
var isIOS = ua.match(/(ipad|iphone|ipod)/g);
var isChrome = ua.indexOf("chrome") ]]> -1;
var offset = new Array();
var sh = screen.availHeight;
var sw = screen.availWidth;
var ih = window.innerHeight;
var iw = window.innerWidth;
var isWebApp = false;
var debug = false; // for now only test for android and iOS, other mobile platforms may be included later
if(isAndroid || isIOS) { // yes we're on a mobile OS
if(isIOS) {
if(window.navigator.standalone) isWebApp = true;
} else {
// it is not IOS
if(isAndroid) {
offset[0] = 0; // <- not all android devices show status header
offset[1] = 25; // <- is android status header
offset[2] = 44; // <- is including error message ssl cert;
// documentation says 25dp = status header
// , up til now all devices show this in javascript as 25px.
// todo check offset for other android devices
} else {
// for future use, if mobile platform other than iOS or android
offset[0] = 0;
}
if(window.orientation==0) {
for (var i = 0; i < offset.length; i++) {
if(ih == (sh - offset[i])) {
if(debug) alert(window.orientation + ' ' + ih + '==' + sh + ' offset:' + offset[i]);
isWebApp = true;
break;
}
}
} else {
if(isAndroid && isChrome) {
// chrome on android doesn't switch values of availHeight and availWidth on orientation landscape
for (var i = 0; i < offset.length; i++) {
if(ih == (sh - offset[i])) {
if(debug) alert(window.orientation + ' ' + ih + '==' + sh + ' offset:' + offset[i]);
isWebApp = true;
break;
}
}
} else {
for (var i = 0; i < offset.length; i++) {
if(ih == (sw - offset[i])) {
if(debug) alert(window.orientation + ' ' + ih + '==' + sw + ' offset:' + offset[i]);
isWebApp = true;
break;
}
}
}
}
}
}
return isWebApp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment