Yep, see the answers above give you a good answer for how to detect a User Agent on the server side. However, as a rule, I think that handling compatability of scripts and styles is much better done on the client side. So, instead of having your cgi write a different line into the web page for Netscape and Internet Explorer, use Javascript to pull in different scripts for different browsers. In Javascript, you can query the navigator object for information about the user agent, and then use this information to link in different stylesheets or external scripts. eg:
if ( document.layers ) {
document.write('<link href="/path/to/netscape_style.css" rel="Style
+sheet" type="text/css">');
}
Why? Well, firstly you can find out more information on the client side than is available on the server - for example, CPU class, OS, or screen resolution. This is particularly useful as Internet Explorer is really quite different in capabilities between Mac and Windows.
But the biggest reason is caching. I got burnt on this once before - we wrote a large content-managed site which had been designed with different stylesheets and Javascript for IE and NN4. We handled it on the server side. Then we got to deploying the site on the client's machines, and we discover that the webserver is going to be sitting behind Squid, a caching proxy. Our application rendered the pages on request, then they'd be stored for 6 hours unchanged in the proxy. So, let's say the homepage was rendered for Netscape - oops, then all IE visitors got the NS version from Squid, and it was broken. oops. We had to re-write to do browser-checking on the client side, as above.
OK, in this example, we did know about the proxy eventually, and could control it. However, proxies are ubiquitous, and you won't know about or be able to control most of them. It'll be much nicer if your page and Javascripts work well even when they've come from a reverse caching proxy, through a company proxy, or get served from Google's cached copy. It won't work if you do browser compatability server-side, but it probably will if you do it in the user's browser.
As an aside, the W3C DOM is a very good thing. Being able to write once for NN6 and IE5, and having Javascript do what it says on the tin has renewed my enjoyment of client-side programming. Open Directory has a bunch of useful links to tutorials and examples which are a bit less dry than the straight W3C recommendations.
a |