Detecting the Browser's
|
|
1. |
var agent = navigator.userAgent. toLowerCase();
Add the variable agent that records the value of the navigator.userAgent object (the full list of compatible browsers) and converts it to lowercase ( Code 13.2 ).
Code 13.2. This code first
|
|
[View full width] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/ |
2.
var isMoz = (agent.indexOf('mozilla') != -1);
3.
if (isMoz isIE isSafari isOpera) {...}
4.
if (isMoz) {...}
5.
else document.write('<p>Sorry, I don\'t recognize this browser.</p> ');
Tips
Some browsers, such as Safari, will show up as being compatible with other browsers ( Figure 13.7 ), so you may need to take that into account when coding.
There are, of course, more than four browsers. Many Web designers used to use browser detection to tailor their work for one browser over others, but this technique is quickly going out of use (see the sidebar "Feature Sensing or Browser Sensing?").
Feature Sensing or Browser Sensing?Browser sensing, also known as browser detection, is often used instead of feature sensing to determine whether a DHTML function should be run in a particular browser. Using browser sensing, however, means that you have to know exactly what code will or will not run in the browsers you are including or excluding.
Using browser sensing to determine DHTML compatibility can cause problems,
|