Home > Web Front-end > JS Tutorial > body text

JavaScript determines browser type and version_javascript skills

WBOY
Release: 2016-05-16 18:55:38
Original
1136 people have browsed it

A few days ago, a little prince was born in the browser family, the Chrome browser launched by Google. Since Chrome was born into a famous family, even though he was still a little guy, no one dared to look down on him. From now on, we often say that the "Four Talents" of browsers have to be renamed the "Five Golden Flowers".
In website front-end development, browser compatibility issues have already made us in a hurry, and the birth of Chrome will cause us more troubles. Browser compatibility is the first problem to be solved by the front-end development framework. To solve the compatibility problem, you must first accurately determine the browser type and its version.
JavaScript is the main language for front-end development. We can determine the type and version of the browser by writing JavaScript programs. There are generally two methods for JavaScript to determine the browser type. One is based on the unique attributes of various browsers, and the other is determined by analyzing the userAgent attribute of the browser. In many cases, after the browser type is determined by value, the browser version needs to be determined to handle compatibility issues, and the browser version can generally only be known by analyzing the browser's userAgent.
Let's first analyze the characteristics of various browsers and their userAgent.
IE
Only IE supports the creation of ActiveX controls, so it has something that other browsers do not have, which is the ActiveXObject function. As long as it is determined that the ActiveXObject function exists in the window object, it can be clearly determined that the current browser is IE. The typical userAgent for each version of IE is as follows:

Copy code The code is 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)

Among them, the version number is the number after MSIE.
Firefox
Each DOM element in Firefox has a getBoxObjectFor function, which is used to obtain the position and size of the DOM element (the corresponding function in IE is the getBoundingClientRect function). This is unique to Firefox. By judging it, you can know that the current browser is Firefox. The userAgents of several versions of Firefox are roughly as follows:
Copy code The code is 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

Among them, the version number is the number after Firefox.
Opera
Opera provides a special browser flag, which is the window.opera attribute. Opera’s typical userAgent is as follows:
Copy code The code is as follows:

Opera/9.27 (Windows NT 5.2; U; zh-cn)
Opera/8.0 (Macintosh; PPC Mac OS X; U; en)
Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0

Among them, the version number is a number close to Opera.
Safari
There is an openDatabase function in the Safari browser that other browsers do not have, which can be used as a sign to judge Safari. A typical Safari userAgent is as follows:
Copy code The code is as follows:

Mozilla/5.0 (Windows; U; 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 Safari/419.3

Its version number is the number after Version.
Chrome
Chrome has a MessageEvent function, but so does Firefox. However, fortunately, Chrome does not have Firefox's getBoxObjectFor function. According to this condition, the Chrome browser can still be accurately determined. Currently, Chrome’s userAgent is:
Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13
Among them, the version number in Chrome is only the number after it.
Interestingly, Chrome's userAgent also includes Safari features. Perhaps this is the basis for Chrome to run all Apple browser applications.
As long as we understand the above information, we can determine the browser type and its version based on these characteristics. We will save the judgment results in the Sys namespace and become the basic flag information of the front-end framework for future programs to read. If the browser is determined, the Sys namespace will have an attribute with the name of the browser, and its value is the version number of the browser. For example, if IE 7.0 is determined, the value of Sys.ie is 7.0; if Firefox 3.0 is determined, the value of Sys.firefox is 3.0. The following is the code to determine the browser:
<script type="text/javascript"> 
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]; 
//以下进行测试 
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


We put the judgment on IE first because IE has the most users, followed by Firefox. Determining browser types in order of number of users can improve the efficiency of judgment and reduce wasted effort. The reason why Chrome is ranked third is because we predict that Chrome will soon become the third browser in terms of market share. Among them, when analyzing the browser version, regular expressions are used to extract the version information.
If you are good at JavaScript, you can also write the previous judgment code like this:

<script type="text/javascript"> 
var Sys = {}; 
var ua = navigator.userAgent.toLowerCase(); 
window.ActiveXObject &#63; Sys.ie = ua.match(/msie ([\d.]+)/)[1] : 
document.getBoxObjectFor &#63; Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1] : 
window.MessageEvent && !document.getBoxObjectFor &#63; Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1] : 
window.opera &#63; Sys.opera = ua.match(/opera.([\d.]+)/)[1] : 
window.openDatabase &#63; Sys.safari = ua.match(/version\/([\d.]+)/)[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


This can make the JavaScript code more streamlined. Of course, the readability is slightly worse, depending on whether you value efficiency or maintainability.
Using different characteristics to determine the browser is faster than using regular expressions to analyze userAgent, but these characteristics may change with browser versions. For example, if a browser's original unique feature achieves market success, other browsers may follow suit and add this feature, causing the browser's unique feature to disappear and causing our judgment to fail. Therefore, a relatively safe approach is to determine the browser type by parsing the characteristics in userAgent. What's more, anyway, judging the version information also requires parsing the browser's userAgent.
By analyzing the userAgent information of various browsers, it is not difficult to find the regular expressions that distinguish various browsers and their versions. Moreover, the judgment of browser type and version can be completely integrated. So, we can write the following code:

<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


Among them, judgment expressions such as "...? ...: ..." are used to simplify the code. The judgment condition is an assignment statement, which not only completes the matching of regular expressions and copy the results, but also directly serves as a conditional judgment. The subsequent version information only needs to be extracted from the previous matching results. This is very efficient code.
The above codes are all pre-research done to build the front-end framework, and have been tested on the five major browsers. From now on, you only need to use if(Sys.ie) or if(Sys.firefox) to judge a certain browser, and you only need to use if(Sys.ie == '8.0') or if(Sys. firefox == '3.0') and other forms are still very elegant to express.
The front-end framework project has been started, everything depends on the process and results...
Original: Li Zhan (leadzen) Ali Software 2008-9-6 Hangzhou

In order to make everyone have more fun, the editor of Script House has compiled a few:

<script language="JavaScript"> 
function getOs() 
{ 
  var OsObject = ""; 
  if(navigator.userAgent.indexOf("MSIE")>0) { 
    return "MSIE"; //ie浏览器
  } 
  if(isChrome=navigator.userAgent.indexOf("Chrome")>0){ 
    return "chrome"; //Chrome浏览器
  } 
  if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ 
    return "Firefox"; //Firefox浏览器
  } 
  if(isOpera=navigator.userAgent.indexOf("Opera")>0) { 
    return "Opera"; //Opera浏览器
  } 
  if(isSafari=navigator.userAgent.indexOf("Safari")>0) { 
    return "Safari"; //Safari浏览器
  }  
  if(isCamino=navigator.userAgent.indexOf("Camino")>0){ 
    return "Camino"; //Camino浏览器
//……增加一些其它代码……
  } 
  if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){ 
    return "Gecko"; 
  } 
} 
alert(navigator.userAgent);
 alert("您的浏览器类型为:"+getOs()); 
</script>
Copy after login

Relatively complete judgment code

<script type="text/javascript"> 
document.write('浏览器判別:');
var OsObject=navigator.userAgent;
// 包含「Opera」文字列 
if(OsObject.indexOf("Opera") != -1) 
{ 
   document.write('您的浏览器是Opera吧?'); 
} 
// 包含「MSIE」文字列 
else if(OsObject.indexOf("MSIE") != -1) 
{ 
   document.write('您的浏览器是Internet Explorer吧?'); 
} 
// 包含「chrome」文字列 ,不过360浏览器也照抄chrome的UA

else if(OsObject.indexOf("Chrome") != -1) 
{ 
  document.write('您的浏览器是chrome或360浏览器吧?'); 
}
// 包含「UCBrowser」文字列 
else if(OsObject.indexOf("UCBrowser") != -1) 
{ 
  document.write('您的浏览器是UCBrowser吧?'); 
}
// 包含「BIDUBrowser」文字列 
else if(OsObject.indexOf("BIDUBrowser") != -1) 
{ 
  document.write('您的浏览器是百度浏览器吧?'); 
}
// 包含「Firefox」文字列 
else if(OsObject.indexOf("Firefox") != -1) 
{ 
  document.write('您的浏览器是Firefox吧?'); 
}
// 包含「Netscape」文字列 
else if(OsObject.indexOf("Netscape") != -1)
{ 
  document.write('您的浏览器是Netscape吧?'); 
} 
// 包含「Safari」文字列 
else if(OsObject.indexOf("Safari") != -1) 
{ 
   document.write('您的浏览器是Safari 吧?'); 
} 
else{ 
  document.write('无法识别的浏览器。'); 
} 
</script> 
Copy after login

Related labels:
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