04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9: Client Detection<br />

}();<br />

return {<br />

engine: engine<br />

};<br />

In this code, a global variable named client is declared to hold the information. Within the anonymous<br />

function is a local variable named engine that contains an object literal with some default settings.<br />

Each rendering engine is represented by a property that is set to 0. If a particular engine is detected,<br />

the version of that engine will be placed into the corresponding property as a floating - point value. The<br />

full version of the rendering engine (a string) is placed into the ver property. This setup allows code<br />

such as the following:<br />

if (client.engine.ie) { //if it’s IE, client.ie is greater than 0<br />

//IE-specific code<br />

} else if (client.engine.gecko > 1.5){<br />

if (client.engine.ver == “1.8.1”){<br />

//do something specific to this version<br />

}<br />

}<br />

Whenever a rendering engine is detected, its property on client.engine gets set to a number greater<br />

than 0, which converts to a Boolean true . This allows a property to be used with an if statement to<br />

determine the rendering engine being used even if the specific version isn ’ t necessary. Since each<br />

property contains a floating - point value, it ’ s possible that some version information may be lost. For<br />

instance, the string ” 1.8.1 ” becomes the number 1.8 when passed into parseFloat() . The ver<br />

property assures that the full version is available if necessary.<br />

To identify the correct rendering engine, it ’ s important to test in the correct order. Testing out of order<br />

may result in incorrect results due to the user - agent inconsistencies. For this reason, the first step is to<br />

identify Opera, since its user - agent string may completely mimic other browsers. Opera ’ s user - agent<br />

string cannot be trusted since it won ’ t, in all cases, identify itself as Opera.<br />

To identify Opera, it ’ s necessary to look for the window.opera object. This object is present in all<br />

versions of Opera 5 and later, and is used to identify information about the browser and to interact<br />

directly with the browser. In versions later than 7.6, a method called version() returns the<br />

browser version number as a string, which is the best way to determine the Opera version number.<br />

Earlier versions may be detected using the user - agent string, since identity masking wasn ’ t supported.<br />

However, since Opera ’ s most recent version at the end of 2007 was 9.5, it ’ s unlikely that anyone is using<br />

a version older than 7.6. The first step in the rendering engine ’ s detection code is as follows:<br />

if (window.opera){<br />

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

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

}<br />

The string representation of the version is stored in engine.ver , and the floating - point representation is<br />

stored in engine.opera . If the browser is Opera, the test for window.opera will return true .<br />

Otherwise, it ’ s time to detect another browser.<br />

The next logical rendering engine to detect is WebKit. Since WebKit ’ s user - agent string contains ” Gecko ”<br />

and ” KHTML ” , incorrect results could be returned if you were to check for those rendering engines first.<br />

241

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

Saved successfully!

Ooh no, something went wrong!