Welcome to WebmasterWorld Guest from 54.224.160.42

Forum Moderators: open

Message Too Old, No Replies

JavaScript Browser Detection

how do you detect browsers + versions?

     
11:15 pm on Dec 21, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 27, 2002
posts:99
votes: 0


I'm going round & round on this...

How can I (in all browsers) use javascript to detect the browser type and version number?

I would like to do a test and make sure the user has

IE 5.0 & above
NS 6.0 & above
AOL 7.0 & above

THANKS!

5:44 am on Dec 22, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


Yes, I imagine you are going round and round. In the realm of client sniffing, beware - there be dragons! Here are some of the issues:

Some browsers, notably Opera, offer the user a chance to spoof the browser's identity as Explorer or several flavors of Mozilla.

Some browser sniffers will test for the presence of a DOM object and assume the browser's identity from that bit of information. This worked OK for a while and for some objects (document.layers, for example, is a sure sign of Netscape 4) but with more recent browsers, object detection gets very complex.

Even if the user agent string tells you that you have a recent AOL browser, with AOL 8, the Windows version is based on Explorer, but the Mac version is based on Netscape. The differences can give you some surprises.

So, the real question is why do you want to detect the browser and version - for what code? Are you actually going to serve a different page to the older browsers?

There is a (now aging) browser sniffer on our generic javascript thread, message 18 [webmasterworld.com]. Mike Foster did some heroic work creating it, but it still doesn't account for AOL.

I personally stopped browser sniffing. It just got too crazy. Instead, I write W3C valid code, check the rendering on a few browsers and then let it go.

A SORT OF ANSWER
Now, for your specific test - if all you care about is that the browser is at least as recent as one of those three, then this should detect that fact for you with relative safety:

if(document.getElementById)

Well, I take that back, just a bit.

Testing for getElementById will give you IE5.5 and above (which I assume is really what you want - decent DOM support). However, IE5.5 doesn't offer the fullest DOM1 support, so you should still test your code live in that browser.

Sorry if I've just added to the confusion. It is pretty chaotic. That's why I stopped doing it. Another way out is, if you want to use some particular DOM object in DHTML, test for that object. Then you know your code will work- and you don't need to know the specific browser at all.

[edited by: tedster at 5:41 am (utc) on Oct. 4, 2003]

10:44 am on Dec 22, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 28, 2002
posts:492
votes: 0


Have a look at:

[mozilla.org...]

Heed the warning, but it might be a good base to work on.

Tedster is right, though. Browser sniffing is not always the best way to go.

10:57 pm on Dec 22, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 10, 2001
posts:793
votes: 0


Tedster makes some great points and nails it again.

Just a couple of corrections though, its...

if(document.getElementById)
- note the lowercase 'd' at the end and it's supported in IE5.0+

With Xmas Cheer

Josh

1:22 am on Dec 23, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 21, 2002
posts:1051
votes: 0


newnewbie1,

Something else to consider. Some users turn off Javascript so a Javascript sniffer doesn't always work.

Server-side sniffing is another possibility, and it also means you can use some well-tested free downloads.

I use phpSniff, but there is also a perl sniffer available.