homepage Welcome to WebmasterWorld Guest from 54.227.41.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JavaScript Browser Detection
how do you detect browsers + versions?
newnewbie1




msg:1474665
 11:15 pm on Dec 21, 2002 (gmt 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!

 

tedster




msg:1474666
 5:44 am on Dec 22, 2002 (gmt 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]

seindal




msg:1474667
 10:44 am on Dec 22, 2002 (gmt 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.

joshie76




msg:1474668
 10:57 pm on Dec 22, 2002 (gmt 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

HarryM




msg:1474669
 1:22 am on Dec 23, 2002 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved