Created
April 27, 2018 11:41
-
-
Save muhammadfaizan/06587eebeef182180d57699fe405f303 to your computer and use it in GitHub Desktop.
Device detection on mobile theme with Javascript
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
/** | |
* 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