CSS JS to determine browser type and browser version

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax browser browser version code content document firefox function

The following provides some of the Web page effects to determine the browser type and browser version information of the code, he can analyze the browser has mozilla,ie,firefox,opera,safari,chrome and other information oh.


<!doctype HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.jzread.com/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">


<html xmlns= "http://www.w3.org/1999/xhtml" >


<head>


<meta http-equiv= "Content-type content=" HTML; charset=gb2312 "/>


<title>css Tutorial JS judge browser type with browser version </title>


<script type= "text/web Effects" >


var sys = {};


var ua = navigator.userAgent.toLowerCase ();


if (window.activexobject)


sys.ie = Ua.match (/msie ([D.] +/) [1]


else if (document.getboxobjectfor)


Sys.firefox = Ua.match (/firefox/([D.] +/) [1]


else if (window.messageevent &&!document.getboxobjectfor)


sys.chrome = Ua.match (/chrome/([D.] +/) [1]


else if (Window.opera)


Sys.opera = Ua.match (/opera. [D.] +/) [1]


else if (window.opendatabase)


Sys.safari = Ua.match (/version/([D.] +)/) [1];


       


The following test


if (sys.ie) document.write (' ie: ' +sys.ie);


if (sys.firefox) document.write (' Firefox: ' +sys.firefox);


if (sys.chrome) document.write (' Chrome: ' +sys.chrome);


if (sys.opera) document.write (' Opera: ' +sys.opera);


if (Sys.safari) document.write (' Safari: ' +sys.safari);

</script>

</head>

<body>
ie

Only IE supports creating ActiveX controls, so she has something that other browsers do not have, that is, the ActiveXObject function. As long as you judge the Window object exists ActiveXObject function, you can clearly determine the current browser is ie. The typical useragent of IE versions are as follows:


mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)
mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
mozilla/4.0 (compatible; MSIE 5.0; Windows NT)


Where the version number is the number after the MSIE.

Firefox

The DOM elements in Firefox have a getboxobjectfor function to get the position and size of the DOM element (ie corresponds to the getboundingclientrect function). This is unique to Firefox, judge it can know is the current browser is Firefox. Firefox several versions of the useragent are roughly as follows:

mozilla/5.0 (Windows; u; Windows NT 5.2) gecko/2008070208 firefox/3.0.1
mozilla/5.0 (Windows; u; Windows NT 5.1) gecko/20070309 firefox/2.0.0.3
mozilla/5.0 (Windows; u; Windows NT 5.1) gecko/20070803 firefox/1.5.0.12
The version number is the number after Firefox.


Opera

Opera offers a special browser flag, which is the Window.opera property. Opera's typical useragent is as follows:


opera/9.27 (Windows NT 5.2 u; zh)
opera/8.0 (Macintosh; PPC Mac OS X; u; en)
mozilla/5.0 (Macintosh; PPC Mac OS X; u; en) Opera 8.0


The version number is the number near opera.


Safari


Safari browser has a opendatabase function that is not available in other browsers and can be used as a marker for safari. Safari's typical useragent is as follows:


mozilla/5.0 (Windows; Windows NT 5.2) applewebkit/525.13 (khtml, like Gecko) version/3.1 safari/525.13
mozilla/5.0 (iphone; u; CPU like Mac OS X) applewebkit/420.1 (khtml, like Gecko) version/3.0 Mobile/4a93


The version number is the number after version.

Chrome

Chrome has a messageevent function, but Firefox does. However, the good news is that Chrome does not have Firefox's getboxobjectfor function, which can be used to accurately determine the Chrome browser. At present, the useragent of Chrome is:


mozilla/5.0 (Windows; Windows NT 5.2) applewebkit/525.13 (khtml, like Gecko) chrome/0.2.149.27 safari/525.13


</body>
</html>

JavaScript determines browser version

<html>
<head>
<meta http-equiv= "Content-type" content= "HTML; charset=gb2312 ">
<title>js Judgment Browser </title>
</head>
<body>
<hr>
<!--to determine the visitor's browser (type) and version-->
<script type= "Text/javascript" >

var browser=navigator.appname
var b_version=navigator.appversion
var version=parsefloat (b_version)
document.write ("Browser name:" + browser)
document.write ("
")
document.write ("browser version:" + version)
</script>
Other information

<script type= "Text/javascript" >

document.write ("Browser:")
document.write (Navigator.appname + "")

document.write ("Browserversion:")
document.write (navigator.appversion + "")

document.write ("Code:")
document.write (Navigator.appcodename + "")

document.write ("Platform:")
document.write (Navigator.platform + "")

document.write ("Cookies enabled:")
document.write (navigator.cookieenabled + "")

document.write ("Browser ' s user Agent header:")
document.write (navigator.useragent + "")
</script>
CSS to determine user browser version information

<!--[if Ie]>
      <h1> you are using IE browser </h1>
       <!--[if IE 5]>
          <h2> version 5</h2
      <![ Endif]-->
      <!--[if IE 5.0]>
           <h2> version 5.0</h2>
      <![ Endif]-->
      <!--[if IE 5.5]>
           <h2> version 5.5</h2>
      <![ Endif]-->
      <!--[if IE 6]>
           <h2> version 6</h2>
      <![ Endif]-->
      <!--[if IE 7]>
           <H2≫ version 7</h2>
      <![ Endif]-->
<![ Endif]-->

Related Article

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.