Home >Web Front-end >JS Tutorial >Javascript implementation to obtain browser version and browser type_javascript skills
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.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? 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>
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).*?([\d.]+)/; var m = ua.match(re); Sys.browser = m[1].replace(/version/, "'safari"); Sys.ver = m[2]; return Sys; }
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>
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.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? 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).*?([\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>
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.