Skip to content

Instantly share code, notes, and snippets.

@muhammadfaizan
Created April 27, 2018 11:41
Show Gist options
  • Save muhammadfaizan/06587eebeef182180d57699fe405f303 to your computer and use it in GitHub Desktop.
Save muhammadfaizan/06587eebeef182180d57699fe405f303 to your computer and use it in GitHub Desktop.
Device detection on mobile theme with Javascript
/**
* Determine the mobile operating system.
* This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
*
* @returns {String}
*/
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
// iOS detection from: http://stackoverflow.com/a/9039885/177710
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "unknown";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment