Five methods to obtain information such as the browser version

Source: Internet
Author: User

Method 1: Use the browserinformation class.
Method 2: Use the httpbrowsercapabilities class.
Method 3: use JavaScript to explain the User-Agent string of the browser.
Method 4: use JavaScript to determine the current rendering mode of the IE rendering engine.
Method 5: Use HTML extension comments.

Method 1: Use the browserinformation class.
========================================================== =
Applicable to SilverlightProgram.
Provides general information about browsers, such as names, versions, and operating systems.

The following example shows how the browserinformation class provides browser information.

Using system;
Using system. Windows. controls;
Using system. Windows. browser;

Public Class Example
{
Public static void demo (system. Windows. Controls. textblock outputblock)
{
Outputblock. Text + =
"\ Nsilverlight can provide browser information: \ n" +
"\ Nbrowser name =" + htmlpage. browserinformation. Name +
"\ Nbrowser version =" + htmlpage. browserinformation. browserversion. tostring () +
"\ Nuseragent =" + htmlpage. browserinformation. useragent +
"\ Nplatform =" + htmlpage. browserinformation. Platform +
"\ Ncookiesenabled =" + htmlpage. browserinformation. cookiesenabled. tostring () +
"\ Nproductname =" + htmlpage. browserinformation. productname. tostring () +
"\ Nproductversion =" + htmlpage. browserinformation. productversion. tostring ();
}
}

IE8 running result:

Silverlight can provide browser information:

Browser name = Microsoft Internet Explorer
Browser version = 4.0
Useragent = Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; slcc2 ;. net CLR 2.0.50727 ;. net CLR 3.5.30729 ;. net CLR 3.0.30729; Media Center PC 6.0 ;. net CLR 1.1.4322 ;. net4.0c ;. net4.0e)
Platform = Win32
Cookiesenabled = true
Productname = MSIE
Productversion = 8.0

Safari running result:

Silverlight can provide browser information:

Browser name = Netscape
Browser version = 5.0
Useragent = Mozilla/5.0 (Windows NT 6.1) applewebkit/534.50 (khtml, like gecko) version/5.1 Safari/534.50
Platform = Win32
Cookiesenabled = true
Productname = Safari
Productversion = 534.50

Method 2: Use the httpbrowsercapabilities class.
========================================================== =
Applicable to ASP. NET programs.

System. Web. httpbrowsercapabilities browser = request. browser;
String S = "browser capabilities \ n"
+ "Type =" + browser. Type + "\ n"
+ "Name =" + browser. browser + "\ n"
+ "Version =" + browser. Version + "\ n"
+ "Major version =" + browser. majorversion + "\ n"
+ "Minor version =" + browser. minorversion + "\ n"
+ "Platform =" + browser. Platform + "\ n"
+ "Is Beta =" + browser. Beta + "\ n"
+ "Is crawler =" + browser. crawler + "\ n"
+ "Is AOL =" + browser. AoL + "\ n"
+ "Is Win16 =" + browser. Win16 + "\ n"
+ "Is Win32 =" + browser. Win32 + "\ n"
+ "Supports frames =" + browser. frames + "\ n"
+ "Supports tables =" + browser. Tables + "\ n"
+ "Supports cookies =" + browser. Cookies + "\ n"
+ "Supports VBScript =" + browser. VBScript + "\ n"
+ "Supports JavaScript =" + browser. Javascript + "\ n"
+ "Supports Java applets =" + browser. javaapplets + "\ n"
+ "Supports ActiveX controls =" + browser. activexcontrols + "\ n ";

 

Httpbrowsercapabilities B = request. browser;
Response. Write ("browser name and version:" + B. Type + "<br> ");
Response. Write ("name:" + B. browser + "<br> ");
Response. Write ("version:" + B. Version + "<br> ");
Response. Write ("operating platform:" + B. Platform + "<br> ");
Response. Write ("supported frameworks:" + B. frames + "<br> ");
Response. Write ("table supported:" + B. Tables + "<br> ");
Response. Write ("whether cookies are supported:" + B. Cookies + "<br> ");

Browser name and version: IE6
Name: IE
Version: 6.0
Operating Platform: WINXP
Supported framework: True
Table supported: True
Whether cookies are supported: True

 

If (page. Request. browser. ecmascriptversion. Major> 0 & page. Request. browser. w3cdomversion. Major> 0) // you can check whether scripts are supported.
{
Response. Write ("browser name and version:" + Page. Request. browser. Type + "<br/> ");
Response. Write ("browser version:" + Page. Request. browser. Version + "<br/> ");
Response. Write ("main browser version:" + Page. Request. browser. majorversion + "<br/> ");
Response. Write ("browser name:" + Page. Request. browser. browser );
}

 

method 3: use JavaScript to explain the browser's User-Agent string:
================================================= ===============< br> View plaincopy to clipboardprint?
function getinternetexplorerversion ()
// returns the version of Internet Explorer or A-1
// (indicating the use of another browser ).
{< br> var Rv =-1; // return value assumes failure.
If (navigator. appname = 'Microsoft Internet Explorer ')
{< br> 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);
}< br> return RV;
}< br> function checkversion ()
{< br> var MSG = "you're not using Internet Explorer. ";
var ver = getinternetexplorerversion ();

If (ver>-1)
{< br> If (ver> = 8.0)
MSG = "you're using a recent copy of Internet Explorer. "
else
MSG =" You shoshould upgrade your copy of Internet Explorer. ";
}< br> alert (MSG);
}< br> function getinternetexplorerversion ()
// returns the version of Internet Explorer or A-1
// (indicating the use of another browser ).
{< br> var Rv =-1; // return value assumes F Ailure.
If (navigator. appname = 'Microsoft Internet Explorer ')
{< br> 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);
}< br> return RV;
}< br> function checkversion ()
{< br> 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 );
}

 

Method 4: use JavaScript to determine the current rendering mode of the IE rendering engine:
========================================================== ==============

View plaincopy to clipboardprint?
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.
}

 

 

Method 5: Use the HTML extension comment statement:
========================================
<! -- [If! IE] -->

You're not using Internet Explorer.

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.