[thelist] Browser Detect with JavaScript

Peter-Paul Koch gassinaumasis at hotmail.com
Mon Dec 4 02:32:08 CST 2000


>    // Disallow IE 3
>     var agt=navigator.userAgent.toLowerCase();
>   var is_ie=(agt.indexOf("msie") !=-1);
>   if(is_ie==true) { var
>is_minor=parseFloat(agt.substring(agt.indexOf('msie')+4,(agt.indexOf('msie')+11)));}
>   if(is_ie==true && is_minor<4) {resultck=false;}
>
>   if(resultck==false){
>     document.write('You Web browser sucks!');
>   }
>}

This check can be done far simpler. If you want a message to appear in 
non-CSS browsers (including IE3), do:

<P STYLE="display: none">Your web browser is not the latest and 
greatest.</P>

The CSS browsers will honour the display: none and remove the P from the 
document, while non-CSS browsers will show it. IE3 doesn't support display, 
so it also shows the message.

>I am not checking for document.layers and document.images since I
>don't want to consider these implementations compatible:
>
>However if I want to allow just the latest versions of Netscape 4 I
>could work in:
>
>var is_nav=((agt.indexOf("mozilla")!=-1) &&
>(agt.indexOf("spoofer")==-1) && (agt.indexOf("compatible")==-1));
>var is_minor=parseFloat(navigator.appVersion);
>if((is_nav==true)&&((is_minor>=4.7) || (is_minor==4.08))){resultck=true;}

Why not

if (document.layers && navigator.appVersion.indexOf('4.7') != -1) resultck = 
true;

Far simpler.

I wouldn't rely too much on the 4.08 detect, it might sneak in some versions 
of 4.08 that are non-compatible (after all, the horrid 4.5 and 4.6 versions 
also used NN4.08 as their WWW browser).

ppk

_____________________________________________________________________________________
Get more from the Web.  FREE MSN Explorer download : http://explorer.msn.com





More information about the thelist mailing list