lobo_tuerto's notes
Home
Blog
Notes
About

Identify a mobile browser with JavaScript

How to tell if someone is using a cell phone to browser your site.

📅Published25 April 2020Last updatedApr 2023
🏷️
frontendjavascript

After some research, I have found that the easiest and more reliable way to tell if the current browser is on a mobile device is with this:

Source code

function isMobile () {
  return /Mobi/.test(window.navigator.userAgent)
}

Just as the MDN says on the topic:

In summary, we recommend looking for the string “Mobi” anywhere in the User Agent to detect a mobile device.

Easy peasy, lemon squeezy! 🍋


Got comments or feedback?
Follow me on
v-57f6bb4