Detect browser typeTag(s): Varia
Detecting if a feature is present or not
We test for a known feature to decide the browser type.
ns = (document.layers) ? true : false;
ie = (document.all) ? true : false;
ff = document.getElementById? true : false;
if (ie) document.write("Internet Explorer");
else if (ff) document.write("Firefox");
else if (ns) document.write("Netscape");
else document.write("Unknown browser !!!");
Use a conditionnal comment
See this HowToUse the browser name
This technique is not very good because a browser can alter its name to allow a better compatibility.<HTML>
<HEAD>
<SCRIPT LANGUAGE= "JavaScript">
function isNetscape(v) {
/*
** Check if the browser is Netscape compatible
** v version number
** returns true if Netscape and version equals or greater
*/
return isBrowser("Netscape", v);
}
function isMicrosoft(v) {
/*
** Check if the browser is Microsoft Internet Explorer compatible
** v version number
** returns true if MSIE and version equals or greater
*/
return isBrowser("Microsoft", v);
}
function isBrowser(b,v) {
/*
** Check if the current browser is compatible
** b browser name
** v version number (if 0 don't check version)
** returns true if browser equals and version equals or greater
*/
browserOk = false;
versionOk = false;
browserOk = (navigator.appName.indexOf(b) != -1);
if (v == 0) versionOk = true;
else versionOk = (v <= parseInt(navigator.appVersion));
return browserOk && versionOk;
}
</SCRIPT></HEAD><BODY><FORM>
<INPUT TYPE="button"
VALUE="Test for Netscape 4"
onClick="alert(isBrowser('Netscape', 4));">
<INPUT TYPE="button"
VALUE="Test for IE3"
onClick="alert(isBrowser('Explorer', 0));">
</FORM></BODY></HTML>
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com