JS Coding Questions Logo
JS Coding Questions
#168💼 Interview💻 Code

How do you detect a mobile browser without regexp

Advertisement

728x90

You can detect mobile browsers by simply running through a list of devices and checking if the useragent matches anything. This is an alternative solution for RegExp usage,

javascript
1function detectmob() {
2  if (
3  navigator.userAgent.match(/Android/i) ||
4  navigator.userAgent.match(/webOS/i) ||
5  navigator.userAgent.match(/iPhone/i) ||
6  navigator.userAgent.match(/iPad/i) ||
7  navigator.userAgent.match(/iPod/i) ||
8  navigator.userAgent.match(/BlackBerry/i) ||
9  navigator.userAgent.match(/Windows Phone/i)
10  ) {
11  return true;
12  } else {
13  return false;
14  }
15  }

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 83

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
168of476