...

View Full Version : decent browser detection



homerUK
11-10-2004, 12:03 PM
I know this is probably a stupid question which is asked a million times... but anyone got a good reliable browser detection script? I've written the following:



var useragent = navigator.userAgent;
var browser_name = navigator.appName;

if (useragent.indexOf('MSIE') > -1) { alert("we have IE"); }
if ((useragent.indexOf('Mozilla') > -1) && (useragent.indexOf('MSIE') < 0)) { alert("we have mozilla!"); }



but I'm not sure it is fool proof!!
Cheers...

lavalamp
11-10-2004, 12:36 PM
There are many many browsers out there, you cannot possibly detect them all so why bother trying? You were right, the script you have is not fool-proof, it will group Opera into the same category as IE.

Why do you need to detect browsers anyway? There are very few (if any) scripts that you actually need to detect browsers for.

homerUK
11-10-2004, 01:42 PM
I've got a free WYSIWYG editor (www.textarearich.com) and I need to now make a version which will work in Mozilla... I have the code, I just need to sniff for either IE or Mozilla - any others I can throw out for now cos I cant code for them yet!!

lavalamp
11-10-2004, 02:00 PM
That's not really the best way to go about things, just tweak your layout so that it is acceptable in both browsers. So long as the content is delivered to the user that's all that matters.

jbot
11-10-2004, 03:07 PM
So long as the content is delivered to the user that's all that matters.

well said. anything else is just frivolous. if it's still needed then, object and/or method detection will iron out browser differences.

homerUK
11-10-2004, 03:16 PM
its for referencing objects and things... it is deffinately needed!

jbot
11-10-2004, 03:22 PM
its for referencing objects and things

it's not needed like that tho. in your functions, you just detect what's supported and then do what's necessary. there's no need to do var isIE6=document.all=true or anything like that:


if (document.whatever) do whatever;
else if (document.somethingElse) do somethingElse;

homerUK
11-10-2004, 03:28 PM
oh right! good thinking...
I was thinking the code would be something like

if (browser == IE)
{
do this....
}
if (browser == Moz)
{
do this then..
}

glad you suggested that!! :) cheers!

jbot
11-10-2004, 04:11 PM
i used to do what you do. but it's kinda not needed really. the only time i ever needed to explicitly differentiate between browsers was when marking up a system to help users activate cookies, JS, etc, which is different from browser to browser. for day-to-day scripting, tho, it's not needed.

liorean
11-10-2004, 04:16 PM
jbot: It depends a lot on the type of script you are using. Advanced UI (from a web perspective) can require quite a lot of trickery to get working. Especially when working with form controls, character inputs, style handling and events.

jbot
11-10-2004, 04:19 PM
jbot: It depends a lot on the type of script you are using. Advanced UI (from a web perspective) can require quite a lot of trickery to get working. Especially when working with form controls, character inputs, style handling and events.

conceded, hence why i mentioned my browser helper system. but inline object detection should be the first port of call. you can then break it down to appName and appVersion later if need be: it's just best not to rely on it, given UA-spoofing etc.

brothercake
11-10-2004, 06:16 PM
I know it's perverse, but I use object and feature detection to detect browsers - it's not completely reliable, but it's much better than UA information, for those times when it really is the browser you need to know. http://www.brothercake.com/site/resources/scripts/sniffer/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum