Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detects mobile devices: phones, tablets. mobileCheck is a lightweight Javascript utils for detecting mobile devices and tablets. Its using User Agent string. Usage: if ( mobileCheck.smarphone ) { // Code }
var mobileCheck = {
ios: (function(){
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
}()),
android: (function(){
return navigator.userAgent.match(/Android/i);
}()),
blackBerry: (function(){
return navigator.userAgent.match(/BB10|Tablet|Mobile/i);
}()),
windows: (function(){
return navigator.userAgent.match(/IEMobile/i);
}()),
smartphone: (function(){
return (window.innerWidth <= 384 && window.innerHeight <= 640);
}()),
tablet: (function(){
return (navigator.userAgent.match(/Tablet|iPad|iPod/i) && window.innerWidth <= 1280 && window.innerHeight <= 800);
}()),
all: (function(){
return navigator.userAgent.match(/Android|BlackBerry|Tablet|Mobile|iPhone|iPad|iPod|Opera Mini|IEMobile/i);
}())
};
@akbortoli

This comment has been minimized.

Copy link

akbortoli commented Nov 7, 2013

Fiz um teste com algumas variações do seu codigo: http://jsperf.com/mobile-simple-detection

@GaurravsIN

This comment has been minimized.

Copy link

GaurravsIN commented Sep 26, 2018

I guess you have done wrong for tablet

tablet: (function(){
    return (navigator.userAgent.match(/Tablet|iPad|iPod/i) && window.innerWidth <= 1280 && window.innerHeight <= 800);
  }()),

Instead It should be

tablet: (function(){
    return (navigator.userAgent.match(/Tablet|iPad|iPod/i) && window.innerWidth <= 1280 && window.innerHeight >= 800);
  }()),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.