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 />

(continued)<br />

var browser = {<br />

//browsers<br />

ie: 0,<br />

firefox: 0,<br />

safari: 0,<br />

konq: 0,<br />

opera: 0,<br />

chrome: 0,<br />

safari: 0,<br />

};<br />

//specific version<br />

ver: null<br />

}();<br />

var system = {<br />

win: false,<br />

mac: false,<br />

x11: false<br />

};<br />

//detection of rendering engines/platforms/devices here<br />

return {<br />

engine: engine,<br />

browser: browser,<br />

system: system<br />

};<br />

This code introduces a new system variable that has three properties. The win property indicates if the<br />

platform is Windows, mac indicates Mac, and x11 indicates Unix. Unlike rendering engines, platform<br />

information is typically very limited, without access to operating systems or versions. Of these three<br />

platforms, browsers regularly report only Windows versions. For this reason, each of these properties is<br />

represented initially by a Boolean false instead of a number (as with the rendering - engine properties).<br />

To determine the platform, it ’ s much easier to look at navigator.platform than to look at the user -<br />

agent string, which may represent platform information differently across browsers. The possible values<br />

for navigator.platform are ” Win32 ” , ” Win64 ” , ” MacPPC ” , ” MacIntel ” , ” X11 ” , and ” Linux i686 ” ,<br />

which are consistent across browsers. The platform - detection code is very straightforward, as you can<br />

see in the following example:<br />

var p = navigator.platform;<br />

system.win = p.indexOf(“Win”) == 0;<br />

system.mac = p.indexOf(“Mac”) == 0;<br />

system.x11 = (p.indexOf(“X11”) == 0) || (p.indexOf(“Linux”) == 0);<br />

This code uses the indexOf() method to look at the beginning of the platform string. Even though<br />

” Win32 ” is currently the only Windows string supported, Windows is moving toward a 64 - bit<br />

architecture that may mean the introduction of a ” Win64 ” platform. To prepare for this, the platform -<br />

detection code simply looks for the string ” Win ” at the beginning of the platform string. Testing for a<br />

248

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

Saved successfully!

Ooh no, something went wrong!