天天看點

檢測IE版本号的方法總結

檢測浏覽器(比如IE)的版本号碼是Web 開發最常遇到的問題之一, 以下總結幾種檢測IE版本号碼的方法:

通過Javascript解釋浏覽器的 User-Agent 字元串:

Javascript代碼

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.exec(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 should 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.exec(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 should upgrade your copy of Internet Explorer.";

}

alert( msg );

}

通過Javascript判斷IE渲染引擎的的目前渲染模式:

Javascript代碼

engine = null;

if (window.navigator.appName == "Microsoft Internet Explorer")

{

// This is an IE browser. What mode is the engine in?

if (document.documentMode) // IE8

engine = document.documentMode;

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.documentMode) // IE8

engine = document.documentMode;

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.

}

通過ASP.NET 的 HttpBrowserCapabilities 對象:

C-sharp代碼

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 should 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 should upgrade your copy of Internet Explorer.";

}

else

msg = "You're not using Internet Explorer.";

Label1.Text = msg;

}

通過HTML的擴充注釋語句:

Xhtml代碼

<!--[if gte IE 8]>

<p>You're using a recent version of Internet Explorer.</p>

<![endif]-->

<!--[if lt IE 7]>

<p>Hm. You should upgrade your copy of Internet Explorer.</p>

<![endif]-->

<![if !IE]>

<p>You're not using Internet Explorer.</p>

<![endif]>

繼續閱讀