Detecting Android Browser, But Not Chrome

“A browser’s user-agent string isn’t an identifier, it’s a reverse-chronological history of the web.” – Jeremy Keith

I have to perform evil browser-sniffing because Android Browser is … special. A little slow and using SoundManager, a little different.

Well, today I installed Ice Cream Sandwich (Android 4.0) on the Nexus today, much hat tips due to the Cyanogenmod wiki guide. So I can finally try out Chrome on Android, and it’s actually sweet. In fact, on first impressions, it has more in common with Chrome on Desktop than it has with the standard Android Browser. So I had to update the evil sniff code to exclude Chrome.

Both browsers present as “Android” and “Mobile”. Chrome additionally presents as “CrMo”. So my new CoffeeScript to detect Android Browser, but not Chrome, is this:

  1. isAndroid = ->
  2.   ua = navigator.userAgent.toLowerCase()
  3.   ua.indexOf("android") != -1
  4.     and ua.indexOf("mobile") != -1
  5.     and ua.indexOf("crmo")==-1

Update: And as Pornelski points out, there’s also Firefox, Opera, et al. How I end up grouping them is still up in the air, so consider this a work in progress.

Originally adapted from here.

6 thoughts on Detecting Android Browser, But Not Chrome

  1. “android” and “mobile” are pretty generic. I wouldn’t be surprised if this included Opera and Firefox too.

    Maybe it would be better to use Android-Browser-specific regular expression rather than build and exclusion list?

  2. @pornelski, very true. I should have mentioned I didn’t have time to research which part of the Android Browser string is specifically Android.

    Needs more testing to see which way I want to go on the other browsers anyway; It might actually be that FF and Chrome are grouped together as “desktop-grade” browsers.

Leave a Reply