HOWTO: Determining Browser Version from a ScriptID: Q167820
|
With the new features that Internet Explorer 4.0 provides, it may be desirable for both client-side and server-side scripts to determine the browser version, so that a Web application can either take advantage of new functionality, or degrade gracefully, depending on the version of the currently-running browser.
// This function returns Internet Explorer's major version number,
// or 0 for others. It works by finding the "MSIE " string and
// extracting the version number following the space, up to the decimal
// point, ignoring the minor version number
<SCRIPT LANGUAGE="JavaSCRIPT">
function msieversion()
{
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )
if ( msie > 0 ) // If Internet Explorer, return version number
return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
else // If another browser, return 0
return 0
}
</SCRIPT>
When checking version numbers, scripts should always use the >= operator, instead of the = operator to ensure compatibility with future versions. Existing scripts that specifically check for userAgent equal to "MSIE 3," for instance, should be changed to use the >= operator so that the scripts recognize Internet Explorer 4.0 as well.
<SCRIPT LANGUAGE="javascript">
if ( msieversion() >= 4 )
document.write ( "This is Internet Explorer 4 or later" );
else if ( msieversion() >= 3 )
document.write ( "This is Internet Explorer 3" );
else
document.write ( "This is another browser" );
</SCRIPT>
http://www.cyscape.com/asp/browscap/Copy it to your %windows%\system32\inetsrv\asp\cmpnts directory and execute a server-side script similar to the example below.
http://www.microsoft.com/backoffice/downloads/moreinfo/bcf.asp
<HTML>
<BODY>
<% Set bc = Server.CreateObject("MSWC.BrowserType") %>
<table border=0 cellspacing="0" cellpadding="5">
<tr>
<% If bc.browser = "IE" and bc.version >= 4 Then %>
<td><a href="chocolate.htm" TARGET="_TEXT"
onmouseover="this.style.color='red'"
onmouseout="this.style.color='purple'">chocolate</a>
</td>
<% Else %>
<td><a href="chocolate.htm" TARGET="_TEXT">chocolate</a></td>
<% End If %>
<td>|</td>
<% If bc.browser = "IE" and bc.version >= 4 Then %>
<td><a href="vanilla.htm" TARGET="_TEXT"
onmouseover="this.style.color='red'"
onmouseout="this.style.color='purple'">vanilla</a>
</td>
<% Else %>
<td><a href="vanilla.htm" TARGET="_TEXT">vanilla</a></td>
<% End If %>
</tr>
</table>
</BODY>
</HTML>
Components Reference in the Active Server Pages (ASP) Roadmap
Additional query words: detect verify check
Keywords : kbIE300 kbIE301 kbIE400 kbIE302
Version : WINDOWS:3.0,3.01,3.02,4.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 26, 1999