The User Agent String and Opera
Detecting Opera
Question: as a Web developer, how do I sniff for Opera?
Answer: you do not. You use capability detection. As discussed in the article, browser name sniffing usually leads to fragile scripts that cannot withstand a browser upgrade. It causes major problems for cross-browser compatibility.
This page is aimed at the developer who already employs capability detection, and who nevertheless wishes to identify Opera, to circumvent a bug, for example, or to insure proper counting of Opera in Web site statistics.
In some cases it is possible to detect if the bug has occurred, and then apply a workaround. This is much more reliable than detecting specific versions, and is the preferred approach. Please report such bugs to Opera here.
The user agent string
The generic user agent string for Opera on desktop is given below:
Opera/$VERSION ($OS; U; $LANGUAGE)
where VERSION is the version of Opera, OS is the operating system, and LANGUAGE is the language. Examples of the Opera user agent string for version 9.20 on Windows Vista and Mac OS X are given below, with English as the language:
- Opera/9.20 (Windows NT 6.0; U; en)
- Opera/9.20 (Macintosh; Intel Mac OS X; U; en)
The Opera browser identifies as Opera by default, but because Opera may be configured to spoof as Internet Explorer or Firefox, some sites and statistics mistakenly count Opera as MSIE or Mozilla. To identify Opera, look for "Opera" in the user agent string. Examples of Opera spoofing the user agent string are given below:
- Mozilla/5.0 ($OS; U; $LANGUAGE; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera $VERSION
- Mozilla/4.0 (compatible; MSIE 6.0; $OS; $LANGUAGE) Opera $VERSION
where VERSION is the version of Opera, OS is the operating system,
| OS | Description |
|---|---|
| X11; FreeBSD | 6.2-RELEASE i386 FreeBSD, Intel processor |
| X11; Linux i686 | Linux, Intel processor |
| Macintosh; Intel Mac OS X | Mac OS X, Intel processor |
| Macintosh; PPC Mac OS X | Mac OS X, PPC processor |
| Windows NT 5.1 | Windows XP |
| Windows NT 6.0 | Windows Vista |
and the LANGUAGE variable takes any of the values offered by RFC 2616.
Search pattern
Notice that in the spoofed version of the user agent string, "Opera" is followed by a space, whereas in the default version of the user agent string, "Opera" is followed by a slash ("/").
The generic regexp pattern for retrieving Opera versions from a UA string is therefore:
Opera[ \/][0-9]+\.[0-9]+
This search pattern is very general, and applies to all versions of Opera on almost all devices, including Opera Mini.
- Opera/$VERSION (J2ME/MIDP; Opera Mini/$CLIENT_VERSION/$SERVER_VERSION; $LANGUAGE; U; ssr)
where
| VERSION | The Opera version number |
|---|---|
| CLIENT_VERSION | The build number of the Opera Mini client |
| SERVER_VERSION | The build number of the Opera Mini transcoder server |
| LANGUAGE | The language configured in the client |
The only case not covered here is the case of masking, when "Opera" does not appear in the user agent string. Masking is site-specific, and only applied in the extreme cases when the Web site is not available to Opera by any other means.
JavaScript navigator properties
Note that if you use JavaScript to retrieve information about the browser, then it is advised to use the UA string and not navigator.appName and navigator.appVersion, because the application name attribute will show MSIE or Netscape if Opera spoofs or masks as IE or Firefox respectively. Other browsers, such as Firefox and Safari, will return Netscape by default, making these attributes unreliable for browser detection.
