Wednesday, October 11, 2006

Detecting the browser using simple javascript function


Where the code used to check for if (ie) { ... }, now I wanted it to check for if (ie6OrLower) { ... }. So how to you tell the difference between IE 6 and IE 7+? You could parse the user-agent string, but I’d rather detect changes in the javascript object model. Here’s what I came up with:

if (typeof document.body.style.maxHeight != "undefined") {
// IE 7, mozilla, safari, opera 9
} else {
// IE6, older browsers
}

This distinguishes between browsers based on the fact that IE 7 knows about the maxHeight css property, whereas previous versions of IE didn’t.

1 Comments:

At 6:14 PM, Blogger marika said...

Cool! And since we usually want to detect more browsers than that, there's always this website: css-resources.com/The-Simplest-Effective-Browser-Sniffer.html

 

Post a Comment

<< Home