04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9: Client Detection<br />

The Gecko version number appears between ” rv: ” and a closing parenthesis, so to extract the version<br />

number, the regular expression looks for all characters that are not a closing parenthesis. The regular<br />

expression also looks for the string ” Gecko/ ” followed by eight numbers. If the pattern matches, then<br />

the version number is extracted and stored in the appropriate properties. Gecko version numbers are<br />

related to Firefox versions as detailed in the following table.<br />

Firefox Version<br />

Minimum Gecko Version<br />

1.0 1.7.5<br />

1.5 1.8<br />

2.0 1.8.1<br />

3.0 1.9<br />

As with Safari and WebKit, matches between Firefox and Gecko version numbers are not exact.<br />

IE is the last rendering engine to detect. The version number is found following ” MSIE ” and before a<br />

semicolon, so the regular expression is fairly simple, as you can see in the following example:<br />

var ua = navigator.userAgent;<br />

if (window.opera){<br />

engine.ver = window.opera.version();<br />

engine.opera = parseFloat(client.ver);<br />

} else if (/AppleWebKit\/(\S+)/.test(ua)){<br />

engine.ver = RegExp[“$1”];<br />

engine.webkit = parseFloat(client.ver);<br />

} else if (/KHTML\/(\S+)/.test(ua)){<br />

engine.ver = RegExp[“$1”];<br />

engine.khtml = parseFloat(client.ver);<br />

} else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)){<br />

engine.ver = RegExp[“$1”];<br />

engine.gecko = parseFloat(client.ver);<br />

} else if (/MSIE ([^;]+)/.test(ua)){<br />

engine.ver = RegExp[“$1”];<br />

engine.ie = parseFloat(client.ver);<br />

}<br />

The last part of this rendering engine ’ s detection script uses a negation class in the regular expression to<br />

get all characters that aren ’ t a semicolon. Even though IE typically keeps version numbers as standard<br />

floating - point values, that won ’ t necessarily always be so. The negation class [^;] is used to allow for<br />

multiple decimal points and possibly letters.<br />

244

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!