Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2013 06:07
Show Gist options
  • Save anonymous/5352184 to your computer and use it in GitHub Desktop.
Save anonymous/5352184 to your computer and use it in GitHub Desktop.
Detecting Current Device with Html/Javascript
<!-- @Reference : http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/ -->
<html>
<head>
<!-- For Syntax Highlighting -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css"></link>
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
<script>
function styleCode() {
if (typeof disableStyleCode != 'undefined') { return; }
var a = false;
$('code').each(function() {
if (!$(this).hasClass('prettyprint')) {
$(this).addClass('prettyprint');
a = true;
}
});
if (a) { prettyPrint(); }
}
$(function() {styleCode();});
</script>
<script>
function checkDevice(){
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry()
|| isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if(isMobile.iOS()) alert('iOS');
else if( isMobile.Android() ) alert('Android');
else if( isMobile.any() ) alert('Mobile');
else alert('You are on Mac or PC');
}
</script>
</head>
<body onload="checkDevice()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment