Home > Web Front-end > JS Tutorial > Javascript implementation to obtain browser version and browser type_javascript skills

Javascript implementation to obtain browser version and browser type_javascript skills

WBOY
Release: 2016-05-16 15:27:55
Original
1686 people have browsed it

I found a better code from the Internet that uses JavaScript to determine the browser and browser version. I will record it here:

<script type="text/javascript">
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) &#63; Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) &#63; Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) &#63; Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) &#63; Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) &#63; Sys.safari = s[1] : 0;

    //以下进行测试
    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>
Copy after login

Encapsulate the above code into a method. The method returns a Sys object. The Sys object encapsulates the browser type and version information, as follows:

function getBrowserInfo(){
  var Sys = {};
  var ua = navigator.userAgent.toLowerCase();
  var re =/(msie|firefox|chrome|opera|version).*&#63;([\d.]+)/;
  var m = ua.match(re);
  Sys.browser = m[1].replace(/version/, "'safari");
  Sys.ver = m[2];
  return Sys;
}
Copy after login

When you need to obtain the browser type and version information, you can use the getBroserInfo method, as follows:

 <script type="text/javascript">
     //获取当前的浏览器信息
     var sys = getBrowserInfo();
     //sys.browser得到浏览器的类型,sys.ver得到浏览器的版本
     document.write(sys.browser + "的版本是:" + sys.ver);
 </script>
Copy after login

The complete test code is as follows:

<!DOCTYPE HTML>
<html>
 <head>
  <title>JavaScript获取浏览器类型与版本</title>
  <script type="text/javascript">
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) &#63; Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) &#63; Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) &#63; Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) &#63; Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) &#63; Sys.safari = s[1] : 0;

    //以下进行测试
    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>
  <script type="text/javascript">
    function getBrowserInfo(){
      var Sys = {};
      var ua = navigator.userAgent.toLowerCase();
      var re =/(msie|firefox|chrome|opera|version).*&#63;([\d.]+)/;
      var m = ua.match(re);
      Sys.browser = m[1].replace(/version/, "'safari");
      Sys.ver = m[2];
      return Sys;
    }
    document.write('<hr/>');
    //获取当前的浏览器信息
    var sys = getBrowserInfo();
    //sys.browser得到浏览器的类型,sys.ver得到浏览器的版本
    document.write(sys.browser + "的版本是:" + sys.ver);
  </script>
 </head>
 
 <body>
  
 </body>
</html>

Copy after login

Run result:

Test results under IE browser:

 

Test results under Google browser:

 

Test results under Firefox:

The above is the detailed code for javascript implementation to obtain the browser version and type, which is carried out for IE browser, Firefox browser, Google browser The test was very successful. You can try it out.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template