detect mobile device by javascript

how to detect mobile device by javascript

While I understand and value the concept of feature detection over browser detection, sometimes the need for knowing whether or not we’re dealing with a mobile device arises.  For in-depth device checking, you can rely on a complex library such as The MobileESP Project.  But for simpler applications, the following snippet can be useful.

Syntax

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  alert('its  a mobile device ')   
}
else{  
    alert('its  a desktop ')  
  
}

Live example