|
||
| Inside Technique : Browser Detection Advanced scripting that targets multiple browser vendors and releases often requires coding to each specific browser. We introduce two techniques for detecting the browser version or support for a particular feature: explicit user agent checking and capabilities detection. Explicit Version CheckingYou can detect the type of browser based on its UA (user agent) string. Every browser release including different platforms has a unique user agent and version string (simplified UA string) allowing you to target code for any specific version or platform. The chart below shows the version string for recent windows explorer and navigator releases. These strings have a well-defined format that is maintained from release to release.
The most common operation on the version string is to access the actual major version number (eg., 4 for 4.01). The easiest way to access the version number for the browser is to
convert the appVersion property to an integer. When converting a string to an integer, JavaScript
automatically removes any integers that exist in a string's prefix.
The second most common operation is to determine whether the browser is Internet Explorer or Netscape Navigator. This
is easily accomplished by checking the appVersion for the identifying string ("MSIE" for explorer, "Nav" for Navigator).
Below is simple code that checks if the browser is Navigator or Explorer:
You can continue with the same technique to test for platform (Win95, WinNT, PPC, etc), and so on allowing you to target code from a broad class of browsers to a specific brand, version, and platform. This technique is the most reliable way to target browsers with different scripts but it can also be the most difficult to maintain. You may need to understand the complete workings of each browser version. However, where we find this technique most useful is when different browsers and platforms interpret the same commands differently requiring you to code around them (for example, a bug in a particular version or platform). In most cases, you can distinguish between browsers based on their capabilities. This approach, called Capabilities Detection, creates much simpler scripts and in many cases is sufficient for distinguishing between different browwsers. Capabilities DetectionCapabilities Detection allows you to use features in the JavaScript language to detect
browser capabilities for a particular object or method. Every object in JavaScript
is an associative array. This means that every object is an array and every property, method, and event on that
object takes up a slot in the array. This means you can use JavaScript's array lookup semantics to
access any member of an object: In addition, every object can dynamically grow with new members and tested for any members existance.
This allows you to test for the existance of any member in the object model, simply by checking if
the member is exposed in the array. The easiest way to understand this is to look at a simple code fragment
that tests for the existance of the Now when you write you can check if the browser supports the Which technique do I use?For checking for support of a particular feature, capabilities detection is the way to go. However, some methods or objects may be supported by both browsers, but have slightly different semantics. When you need to distinguish between how one browser or another works with a particular feature, you need to use browser detection. In some cases you may choose to use both object and browser detection. For example, you may first check if the browser supports a particular command, and then based on the browser use the command in slightly different ways. © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |