Mobile user-agent detection
This document attempts to determine the most reliable way of detecting mobile user
agents. Since the test device is running Windows 2003 CE, only browsers compatible
with this platform are used.
The used browsers are:
Pocket IE 6.0
Opera 8.0u1
Detection through the HTTP_USER_AGENT header
The HTTP_USER_AGENT header of Pocket IE consists of the following:
Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; Smartphone;
240x320; SPV C550; OpVer 8.2.3.8)
The HTTP_USER_AGENT header of Opera consists of the following:
Mozilla/4.0 (compatible; MSIE 6.0; Windows CE) Opera 8.0 [en]
The common value in these strings is ”Windows CE”, so this might be useable for
detection. However, to make the detection more reliable, we need to dig deeper,
since this header can easily be modified or faked.
Detection through the HTTP_ACCEPT header
The HTTP_ACCEPT header of Pocket IE consists of the following:
application/vnd.wap.wmlc, application/vnd.wap.wbxml,
application/vnd.wap.wmlscriptc,
application/vnd.wap.multipart.mixed,
application/vnd.wap.xhtml+xml, application/xhtml+xml,
application/vnd.oma.dd+xml, audio/3gpp, audio/mid, audio/midi,
audio/sp-midi, audio/amr, audio/amr-wb, audio/mp3, audio/mp4,
audio/MP4-LATM, audio/mpeg, audio/wav, audio/x-wav, audio/x-
mp3, audio/x-mpeg, audio/x-mpg, audio/x-ms-wax, audio/x-ms-
wma, audio/x-ms-wav, image/bitmap, image/bmp, image/gif,
image/jpeg, image/jpg, image/pjpeg, image/png,
image/vnd.wap.wbmp, image/wbmp, image/x-bmp, image/x-png,
image/x-windows-bmp, message/rfc822, text/css, text/html,
text/plain, text/vnd.wap.wml, text/x-ical, text/xml, text/xsl,
text/x-vcal, application/java, application/java-archive,
/vnd.sun.j2me.app-descriptor, video/3gpp, video/avi, video/x-
ms-avi, video/mp4, video/x-ms-asf, video/x-ms-asx, video/x-ms-
wm, video/x-ms-wmp, video/x-ms-wmv, video/x-ms-wmx, video/x-
ms-wvx, */*
1
The HTTP_ACCEPT header of Opera consists of the following:
text/html, application/xml;q=0.9, application/xhtml+xml,
image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
From the HTTP_ACCEPT headers sent by IE, the following values are useful:
Header value Possible purpose
application/vnd.wap.wmlc Detection of WAP compatibility
application/vnd.wap.wmlscriptc
application/vnd.wap.multipart.mixed
application/vnd.wap.xhtml+xml
image/vnd.wap.wbmp
image/wbmp
text/vnd.wap.wml
image/gif Detection of HTML compatibility
image/jpg
image/jpeg
image/png
text/html
From the HTTP_ACCEPT headers sent by Opera, the following values are useful:
Header value Possible purpose
image/gif Detection of HTML compatibility
image/jpeg
image/png
text/html
As you can see IE allows for WAP detection through this header; roughly you could
say that WAP compliancy + HTML compliancy = Mobile device.
For Opera, this is different, as we can only detect that it can handle HTML pages
through the HTTP_ACCEPT header.
Note that most WAP-only browsers do send the WML* values, so this can be quite
reliable for detecting support for this technology. On the other hand, both IE and
Opera support WML, so in that sense, there’s no reason to write specific HTML
pages for mobile browsers.
2
Detection through CSS
The following CSS is correctly interpreted by both IE and Opera:
@media handheld {
BODY { color: red; font-size: xx-small }
}
This CSS can be applied to format the page layout for mobile devices as well as
hiding specific sections for mobile and regular web browsers.
Detection through JavaScript
The following JavaScript is handled correctly by both IE and Opera:
document.write('Screen width:', screen.width);
The width property of the screen object returns the width of the display. As a rule
you could say that anything below 640 pixels can be regarded as a mobile device
and can be redirected to a specific location.
Drawback of this detection method, of course, is that JavaScript runs on the client
and can be disabled. In this case, the noscript tag can be used in addition,
showing a message and some links to select from.
But this can get tedious when browsing from a PC (where users tend to customize
default browser settings more often).
Conclusion
So, what’s the most reliable way of detecting a mobile web browser? In short, there is
no full proof method to do this.
But there are some rules which might allow you to catch most exceptions:
1. HTTP_USER_AGENT contains Windows CE:
Mobile browser (IE/Opera);
2. HTTP_ACCEPT contains the substring vnd.wap:
WAP browser (IE/WAP Browsers);
3. HTTP_ACCEPT contains the substring text/html:
HTML browser (IE/Opera);
4. HTTP_ACCEPT contains both substrings vnd.wap and text/html:
Mobile browser (IE);
5. JavaScript screen.width property returns a value below 640:
Mobile browser (IE/Opera);
6. The CSS @media handheld property is parsed:
Mobile browser (IE/Opera).
From these rules, the most reliable ones appear to be rule #2 to detect WAP
compliant browsers and rule #6 to detect handheld devices in general.
3