2

Is there a way with HTML/Javascript to check if a visiter is using a Android phone, Sometimes I see this on Forums, when you open the forum on your Android phone it will have a popup saying that there is a app for that forum, how do people do this?

William L.
  • 3,846
  • 9
  • 53
  • 72
  • See also http://stackoverflow.com/questions/9009774/can-i-look-up-the-user-agent-from-javascript for general info on User Agent strings in JavaScript. – apsillers Aug 26 '12 at 16:57
  • 1
    if(navigator.userAgent.toLowerCase().indexOf("android") > -1){ /* code for android */ } – Krishna Kumar Aug 26 '12 at 17:17

2 Answers2

3

In theory, the following will work.

if (navigator.userAgent.match(/android/ig)) {
    // Android only code
}

This is considered bad practice (UA Sniffing) though. Read Browser Detection is Bad. This shouldn't apply in your case though, as you're using it to look for a certain kind of device, not as a form of feature detection.

Community
  • 1
  • 1
Nate Higgins
  • 2,104
  • 16
  • 21
0

When detecting Android user agents, read this post first: http://android-developers.blogspot.com/2010/12/android-browser-user-agent-issues.html

In short, if an Android user agent does not also include the substring "Mobile", it is a tablet or other large-screen device and the user will probably be irritated with you if you serve them a phone-sized page. :)

adamp
  • 28,862
  • 9
  • 81
  • 69