Click to See Complete Forum and Search --> : help!! browser detection woes...


stunews
04-01-2004, 03:01 PM
i am a relative newbie and am trying out some basic scripts and was wondering what the general consensus is when trying to detect the browser type.
I have been trying to use the if (document.all){do this} else if (document.layers){do that} method and after puzzling for a while i found my netscape 7 will not take this method, if i remove the if(document.layers) leaving the else {do that} it works fine, but is this a good idea??
Anyways, is this method an outdated method as I learnt it from quite an old tutorial??
What is the predominant method for browser detection??

ps-heres my script that won't work in case there is a prob

<HTML>
<HEAD>
<script language="javascript">
function doit() {
if (document.layers){
alert("netscape");}
else if (document.all){
alert("IE");}
}
</script>

</HEAD>
<BODY>
<body onload=doit();>

</body>
</HTML>

Vladdy
04-01-2004, 03:05 PM
Nowadays the only approach to scripting that has positive ROI is:

if(document.getElementById)
{ /* run scripts */
}
/* else the user gets the page without javascript enchancements */

fredmv
04-01-2004, 04:18 PM
I fully agree with Vlad, though I'd probably write it like this:if(typeof document.getElementById != 'undefined')
{
// ...
}