Usage of .match()
function detectMobileBrowser() {
const userAgent = navigator.userAgent
if (
userAgent.match(/Android/i) ||
userAgent.match(/webOS/i) ||
userAgent.match(/iPhone/i) ||
userAgent.match(/iPad/i) ||
userAgent.match(/iPod/i) ||
userAgent.match(/BlackBerry/i) ||
userAgent.match(/Windows Phone/i)) {
return true
}
return false
}
Usage of .includes()
function detectMobileBrowser() {
const userAgent = navigator.userAgent.toLowerCase()
const agents = [
'android',
'webos',
'iphone',
'ipad',
'ipod',
'blackberry',
'windows phone',
]
return agents.some((agent) => userAgent.includes(agent))
}