Checking the version number of a browser (such as IE) is one of the most common problems in Web development. The following summarizes several methods for detecting the version number of IE:
Use JavaScript to explain the browser's User-Agent string:
JavaScript code
Function getinternetexplorerversion ()
// Returns the version of Internet Explorer or A-1
// (Indicating the use of another browser ).
{
VaR Rv =-1; // return value assumes failure.
If (navigator. appname = 'Microsoft Internet Explorer ')
{
VaR UA = navigator. useragent;
VaR Re = new Regexp ("MSIE ([0-9] {1,} [/. 0-9] {0 ,})");
If (re.exe C (UA )! = NULL)
Rv = parsefloat (Regexp. $1 );
}
Return RV;
}
Function checkversion ()
{
VaR MSG = "you're not using Internet Explorer .";
VaR ver = getinternetexplorerversion ();
If (ver>-1)
{
If (ver >=8.0)
MSG = "you're using a recent copy of Internet Explorer ."
Else
MSG = "You shocould upgrade your copy of Internet Explorer .";
}
Alert (MSG );
}
Function getinternetexplorerversion ()
// Returns the version of Internet Explorer or A-1
// (Indicating the use of another browser ).
{
VaR Rv =-1; // return value assumes failure.
If (navigator. appname = 'Microsoft Internet Explorer ')
{
VaR UA = navigator. useragent;
VaR Re = new Regexp ("MSIE ([0-9] {1,} [/. 0-9] {0 ,})");
If (re.exe C (UA )! = NULL)
Rv = parsefloat (Regexp. $1 );
}
Return RV;
}
Function checkversion ()
{
VaR MSG = "you're not using Internet Explorer .";
VaR ver = getinternetexplorerversion ();
If (ver>-1)
{
If (ver >=8.0)
MSG = "you're using a recent copy of Internet Explorer ."
Else
MSG = "You shocould upgrade your copy of Internet Explorer .";
}
Alert (MSG );
}
Use JavaScript to determine the current rendering mode of the IE rendering engine:
JavaScript code
Engine = NULL;
If (window. Navigator. appname = "Microsoft Internet Explorer ")
{
// This is an IE browser. What mode is the engine in?
If (document.doc umentmode) // IE8
Engine = document.doc umentmode;
Else // ie 5-7
{
Engine = 5; // assume quirks mode unless proven otherwise
If (document. compatmode)
{
If (document. compatmode = "css1compat ")
Engine = 7; // standards mode
}
}
// The Engine Variable now contains the document compatibility mode.
}
Engine = NULL;
If (window. Navigator. appname = "Microsoft Internet Explorer ")
{
// This is an IE browser. What mode is the engine in?
If (document.doc umentmode) // IE8
Engine = document.doc umentmode;
Else // ie 5-7
{
Engine = 5; // assume quirks mode unless proven otherwise
If (document. compatmode)
{
If (document. compatmode = "css1compat ")
Engine = 7; // standards mode
}
}
// The Engine Variable now contains the document compatibility mode.
}
Httpbrowsercapabilities object in ASP. NET:
C-sharp code
Private float getinternetexplorerversion ()
{
// Returns the version of Internet Explorer or A-1
// (Indicating the use of another browser ).
Float Rv =-1;
System. Web. httpbrowsercapabilities browser = request. browser;
If (browser. browser = "ie ")
Rv = (float) (browser. majorversion + browser. minorversion );
Return RV;
}
Private void page_load (Object sender, system. eventargs E)
{
String MSG;
Double ver = getinternetexplorerversion ();
If (ver> 0.0)
{
If (ver >=7.0)
MSG = "you're using a recent version of Internet Explorer .";
Else
MSG = "You shocould upgrade your copy of Internet Explorer .";
}
Else
MSG = "you're not using Internet Explorer .";
Label1.text = MSG;
}
Private float getinternetexplorerversion ()
{
// Returns the version of Internet Explorer or A-1
// (Indicating the use of another browser ).
Float Rv =-1;
System. Web. httpbrowsercapabilities browser = request. browser;
If (browser. browser = "ie ")
Rv = (float) (browser. majorversion + browser. minorversion );
Return RV;
}
Private void page_load (Object sender, system. eventargs E)
{
String MSG;
Double ver = getinternetexplorerversion ();
If (ver> 0.0)
{
If (ver >=7.0)
MSG = "you're using a recent version of Internet Explorer .";
Else
MSG = "You shocould upgrade your copy of Internet Explorer .";
}
Else
MSG = "you're not using Internet Explorer .";
Label1.text = MSG;
}
Use the HTML extension comment statement:
XHTML code
<! -- [If gte ie 8]>
<P> you're using a recent version of Internet Explorer. </P>
<! [Endif] -->
<! -- [If lt IE 7]>
<P> Hm. You shoshould upgrade your copy of Internet Explorer. </P>
<! [Endif] -->
<! [If! IE]>
<P> you're not using Internet Explorer. </P>
<! [Endif]>