DEV Community

Discussion on: A simple way to detect if browser is on a mobile device with Javascript

Collapse
 
drgaud profile image
DrGaud

Thank you for this,
I have ended up placing it in a view class as a getter function

get isMobile(){
        // credit to Timothy Huang for this regex test: 
        // https://dev.to/timhuang/a-simple-way-to-detect-if-browser-is-on-a-mobile-device-with-javascript-44j3
        if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
            return true
       }
       else{
            return false
       }
    } 
Enter fullscreen mode Exit fullscreen mode

Honestly thanks for this eloquent regex test.

Collapse
 
timhuang profile image
Timothy Huang

thanks for your improvement! :)

Collapse
 
oskarcodes profile image
Oskar Codes

Make it shorter like this:

function isMobile() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
timhuang profile image
Timothy Huang

Thank you!

Thread Thread
 
mitya profile image
Dima • Edited
const isMobile = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
timhuang profile image
Timothy Huang

Wow, really short! Thanks for your improvement.