»
EnglishFrenchVietnamese

Print - Browser Sniffer Script (ver 1.1) - JavaScriptBank.com

Full version: jsB@nk » Snippet » Browser Sniffer Script (ver 1.1)
URL: http://www.javascriptbank.com/browser-sniffer-script-ver-1-1.html

Browser Sniffer Script (ver 1.1) © JavaScriptBank.comThis browser sniffer detects all the major browsers currently out there, and passes the results on as variables to facilitate your browser detection needs. It differentiates between Windows, Mac and Linux, and the most commonly used DHTML browsers - IE4/5/6, Netscape 4/6/7/other Gecko, Konqueror 2.2+, Safari, Opera 5/6/7. All other browsers are grouped together under a single exclude variable, to protect them from complex scripting.

Full version: jsB@nk » Snippet » Browser Sniffer Script (ver 1.1)
URL: http://www.javascriptbank.com/browser-sniffer-script-ver-1-1.html



JavaScript
<script type="text/javascript">// by BrotherCake - http://www.brothercake.com///CS1.1var exclude=1;var agt=navigator.userAgent.toLowerCase();var win=0;var mac=0;var lin=1;if(agt.indexOf('win')!=-1){win=1;lin=0;}if(agt.indexOf('mac')!=-1){mac=1;lin=0;}var lnx=0;if(lin){lnx=1;}var ice=0;var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;var op5=0;var op6=0;var op7=0;var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){var thisKDE=agt;var splitKDE=thisKDE.split("konqueror/");var aKDE=splitKDE[1].split("; ");var KDEn=parseFloat(aKDE[0]);if(KDEn>=2.2){kde=1;ns6=1;exclude=0;}}else if(agt.indexOf('webtv')!=-1){exclude=1;}else if(typeof window.opera!="undefined"){exclude=0;if(/opera[/ ][5]/.test(agt)){op5=1;}if(/opera[/ ][6]/.test(agt)){op6=1;}if(/opera[/ ][7-9]/.test(agt)){op7=1;}}else if(typeof document.all!="undefined"&&!kde){exclude=0;ie=1;if(typeof document.getElementById!="undefined"){ie5=1;if(agt.indexOf("msie 6")!=-1){ie6=1;dcm=document.compatMode;if(dcm!="BackCompat"){com=1;}}}else{ie4=1;}}else if(typeof document.getElementById!="undefined"){exclude=0;if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}}else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){exclude=0;ns4=1;if(typeof navigator.mimeTypes['*']=="undefined"){exclude=1;ns4=0;}}if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}</script>


HTML
<H3>Variables from the script</H3>      <P>The script returns a set of browser variables (Below are <B>live</B>       results dependant on your browser type):       <UL>        <TABLE cellSpacing=3 cellPadding=0 border=0>          <TBODY>          <TR>            <TD vAlign=top>var</TD>            <TD vAlign=top><EM>returns</EM></TD>            <TD vAlign=top><B>Description</B></TD></TR>          <TR>            <TD vAlign=top width=75></TD>            <TD vAlign=top width=75></TD>            <TD vAlign=top></TD></TR>          <TR>            <TD vAlign=top>ie</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ie);</SCRIPT>              </EM></TD>            <TD vAlign=top>Internet Explorer 4+ and IE-based third-party               browsers. You can also be more specific: </TD></TR>          <TR>            <TD vAlign=top>ie4</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ie4);</SCRIPT>              </EM></TD>            <TD vAlign=top>... Internet Explorer 4 only. </TD></TR>          <TR>            <TD vAlign=top>ie5</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ie5);</SCRIPT>              </EM></TD>            <TD vAlign=top>... Internet Explorer 5 or 6. </TD></TR>          <TR>            <TD vAlign=top>ie6</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ie6);</SCRIPT>              </EM></TD>            <TD vAlign=top>... Internet Explorer 6 only. </TD></TR>          <TR>            <TD colSpan=3><BR> </TD></TR>          <TR>            <TD vAlign=top>ns4</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ns4);</SCRIPT>              </EM></TD>            <TD vAlign=top>Netscape 4 </TD></TR>          <TR>            <TD colSpan=3><BR> </TD></TR>          <TR>            <TD vAlign=top>ns6</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ns6);</SCRIPT>              </EM></TD>            <TD vAlign=top>Gecko and KDE-based browsers - which includes               Netscape 6 and 7, Mozilla, Konqueror and Safari. You can also               identify smaller groups within this:</TD></TR>          <TR>            <TD vAlign=top>ns7</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(ns7);</SCRIPT>              </EM></TD>            <TD vAlign=top>... Netscape 7.           <TR>            <TD vAlign=top>mz7</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(mz7);</SCRIPT>              </EM></TD>            <TD vAlign=top>... any gecko browser <B>except</B> Netscape. This               is principally designed to identify Mozilla's own builds from               Version 0.6 onwards, but it also returns true for any other               non-netscape gecko browser. </TD></TR>          <TR>            <TD vAlign=top>kde</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(kde);</SCRIPT>              </EM></TD>            <TD vAlign=top>... Konqueror, from KDE 2.2 onwards.           <TR>            <TD vAlign=top>saf</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(saf);</SCRIPT>              </EM></TD>            <TD vAlign=top>... Safari. This variable will identify Safari               irrespective of which browser it's set to identify as. </TD></TR>          <TR>            <TD colSpan=3><BR> </TD></TR>          <TR>            <TD vAlign=top>op5</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(op5);</SCRIPT>              </EM></TD>            <TD vAlign=top>Opera 5 </TD></TR>          <TR>            <TD vAlign=top>op6</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(op6);</SCRIPT>              </EM></TD>            <TD vAlign=top>Opera 6 </TD></TR>          <TR>            <TD vAlign=top>op7</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(op7);</SCRIPT>              </EM></TD>            <TD vAlign=top>Opera 7               <P>These variables will identify Opera irrespective of which               browser it's set to identify as. </P></TD></TR></TBODY></TABLE></UL>      <P>Underpinning these is a safety variable, for protecting legacy browsers:      <UL>        <TABLE cellSpacing=0 cellPadding=0 border=0>          <TBODY>          <TR>            <TD vAlign=top width=75></TD>            <TD colSpan=2></TD></TR>          <TR>            <TD vAlign=top>exclude</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(exclude);</SCRIPT>              </EM></TD>            <TD vAlign=top></TD></TR></TBODY></TABLE></UL>      <P>There are also three OS variables:       <UL>        <TABLE cellSpacing=0 cellPadding=0 border=0>          <TBODY>          <TR>            <TD vAlign=top width=75></TD>            <TD vAlign=top width=75></TD>            <TD vAlign=top></TD></TR>          <TR>            <TD vAlign=top>win</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(win);</SCRIPT>              </EM></TD>            <TD vAlign=top>Windows </TD></TR>          <TR>            <TD vAlign=top>mac</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(mac);</SCRIPT>              </EM></TD>            <TD vAlign=top>Mac OS </TD></TR>          <TR>            <TD vAlign=top>lin</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(lin);</SCRIPT>              </EM></TD>            <TD vAlign=top>Linux, or anything else </TD></TR></TBODY></TABLE></UL>      <P>and you can query a lower-case version of the user agent string:      <UL>        <TABLE cellSpacing=0 cellPadding=0 border=0>          <TBODY>          <TR>            <TD vAlign=top width=75></TD>            <TD colSpan=2></TD></TR>          <TR>            <TD vAlign=top>agt</TD>            <TD vAlign=top><EM>              <SCRIPT>document.write(agt);</SCRIPT>              </EM></TD>            <TD vAlign=top></TD></TR></TBODY></TABLE></UL>      <P>The sniffer variables are global, and therefore available to any other       scripts on the same page. They allow you to code for or exclude specific       browsers, eg: </P><PRE>if (mac&&ie5) { ... internet explorer 5 on a mac ... }if (!ns4) { ... not netscape 4 ... }if (win&&(op5||op6)) { ... a windows version of opera 5 or 6 ... }if (ie5||ns6||op7) { ... ie5+, gecko, kde or opera 7 ... }</PRE>      <P>Remember that some browsers return true for more than one variable:       <UL>        <LI>Explorer 6 returns true for (ie6) and also for (ie)         <LI>Explorer 5 or 6 return true for (ie5) and also for (ie)         <LI>Explorer 4 returns true for (ie4) and also for (ie)         <LI>Konqueror returns true for (kde) and also for (ns6)         <LI>Safari returns true for (saf) and also for (ns6)         <LI>All Gecko-based browsers which aren't Netscape return true for (mz7)         and also for (ns6) </LI></UL>      <P>The exclude variable returns true for all unspecified browsers. This is       very useful for protecting them from scary DHTML, eg: </P><PRE>if (!exclude) { ... modern browsers only ... }</PRE>      <P>You may find it necessary to use a combination of expressions to get to       the browser you need, eg: </P><PRE>if (!exclude) {    if (ns6||op7) { ... gecko, KTHML or Opera 7 ... }    else if (ie5) { ... internet explorer 5+ ... }    else { ... any other dhtml browser ... }    }</PRE>      <P>There are several minor browsers which have enough DHTML support to       pass one of the object tests, but not enough to be useful. The script       specifically filters out (adds to the exclude group) - Espial Escape,       ICEBrowser and OmniWeb.