Created
March 29, 2012 15:16
-
-
Save tikitikipoo/2238379 to your computer and use it in GitHub Desktop.
スマホを判別するのに便利なJSスニペット
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
// http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/ | |
var isMobile = { | |
Android: function() { | |
return navigator.userAgent.match(/Android/i) ? true : false; | |
}, | |
BlackBerry: function() { | |
return navigator.userAgent.match(/BlackBerry/i) ? true : false; | |
}, | |
iOS: function() { | |
return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false; | |
}, | |
Windows: function() { | |
return navigator.userAgent.match(/IEMobile/i) ? true : false; | |
}, | |
any: function() { | |
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows()); | |
} | |
}; | |
// Examples | |
// To check to see if the user is on any of the supported mobile devices: | |
if( isMobile.any() ) alert('Mobile'); | |
//To check to see if the user is on a specific mobile device: | |
if( isMobile.iOS() ) alert('iOS'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment