Layout engine with content model
Browser Detection
The most common pattern in your JavaScript scripts is probably the section that determines the browser type. And its structure is probably something like:
Try this script. Convince yourself it's working as expected in Internet Explorer and in Netscape Navigator. Try now in Netscape 6. You should get the
"Unrecognized Browser Detection"
message. Netscape 6 does not supportdocument.all
, nor does it supportdocument.layers
. Netscape 6 supportsdocument.getElementById
. But Internet Explorer supports this method as well, so you need to be careful how to write your new browser sniffer. See Webreference's updated sniffer for example. The script above should look like this now:
Try this script. Notice the positive identification of Netscape 6.
<!-- function oldSniffer() { if (document.all) { document.write("Internet Explorer Detected"); } else if (document.layers) { document.write("Netscape Navigator Detected"); } else { document.write("Unrecognized Browser Detected"); } } function newSniffer() { if (document.all) {
document.write("Internet Explorer Detected"); } else if (document.layers) { document.write("Netscape Navigator Detected"); } else if (document.getElementById) { document.write("Netscape 6 Detected"); } else { document.write("Unrecognized Browser Detected"); } } // -->Cross-Browser Scripting