Main Page

Detecting Mozilla

Just as when you are determining the version of a disguised Opera, using a regular expression is the eas-
iest way to extract IE’s version from the user-agent string:
var reIE = new RegExp(“MSIE (\\d+\\.\\d+)”);
Once again, the pattern looks for one or more numbers, followed by a decimal point, followed by one or
more numbers. Putting that expression into practice, you end up with this code:
if (isIE) {
var reIE = new RegExp(“MSIE (\\d+\\.\\d+);”);
var fIEVersion = parseFloat(RegExp[“$1”]);
isMinIE4 = fIEVersion >= 4;
isMinIE5 = fIEVersion >= 5;
isMinIE5_5 = fIEVersion >= 5.5;
isMinIE6 = fIEVersion >= 6.0;
And that’s all it takes to detect Internet Explorer. This code works equally well on Windows and
Macintosh. Next up is IE’s main competitor, Mozilla.
Detecting Mozilla
By now, you should be familiar with how this works. Refresh your memory with the Mozilla user-agent
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4) Gecko/20011128
To be thorough, take a look at the Opera user-agent string when it is disguised as Mozilla 5.0:
Mozilla/5.0 (Windows NT 5.1; U) Opera 7.54
Fortunately, you have plenty of ways to determine that this is Mozilla. The glaring item that is clearly
visible is that the Mozilla user-agent string says
. If you a look at the Opera Mozilla 5.0
, the string does not appear there. Eureka! That makes this easy:
var isMoz = sUserAgent.indexOf(“Gecko”) > -1;
Up until recently, this was enough to determine if the browser was indeed Mozilla. However, as you saw
earlier, KHTML-based browsers have a user-agent string containing the phrase
“like Gecko”
, which
would also return
for this test. So it is necessary to make sure that the browser contains
but is not KHTML-based:
var isMoz = sUserAgent.indexOf(“Gecko”) > -1
&& !isKHTML;
variable is now accurate, so it’s time to move on to the specific versions.
Chapter 8
11_579088 ch08.qxd 3/28/05 11:38 AM Page 242

JavaScript EditorFree JavaScript Editor     Ajax Editor