Summary of methods for detecting IE version

Source: Internet
Author: User

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]>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.