JS determines the browser type to handle browser compatibility

Source: Internet
Author: User


Windows operating system Browser series:

IE Browser series:

Characteristic performance: All "mozilla/" Start, "MSIE x.0" in the X represents its version;
Judgment method: Rough judgment can only search "MSIE x.0" string can be, strict judgment can be retrieved "mozilla/x.0" (Compatibal; MSIE x.0; Windows NT, but generally not necessary
Windows version Firefox:
Feature performance: "mozilla/x.0", including "Windows NT", "gecko/" and "firefox/";
Judgment method: Rough judgment can only retrieve "firefox/" and "Windows NT" string, strictly judge can retrieve "mozilla/", "Windows NT", "gecko/" and "firefox/" four strings;
Windows version Chrome:
Feature performance: "mozilla/x.0", including "Windows NT", "chrome/", including "applewebkit/", "safari/";
Judgment method: Rough judgment can retrieve only "Windows NT" and "chrome/" string, strict judgment can retrieve "mozilla/", "Windows NT", "applewebkit/", "safari/", "chrome/" Five strings;
Windows Edition Opera:
Feature performance: "Opera/" with "Windows NT", "presto/" string;
Judging method: The rough judgment retrieves only "Windows NT" and "opera/" string, strictly judges simultaneously searches "opera/", "Windows NT" and "presto/";
Windows version Safari:
Characteristic performance: "mozilla/" begins with "Windows NT", "applewebkit/", "safari/";
Judgment method: Rough judgment can be retrieved containing "Windows NT", "safari/" also does not contain "chrome/", strict judgment needs to contain "mozilla/", "Windows NT", "applewebkit/", "safari/" But does not contain "chrome/";
Summary: The browser useragent on the Windows operating system contains a "Windows NT" string to represent the Windows operating system.


Example 1

The code is as follows Copy Code

<script>
Get Browser name and version information
function AppInfo () {
var browser = {
Msie:false, Firefox:false, Opera:false, Safari:false,
Chrome:false, Netscape:false, appname: ' Unknown ', version:0
},
useragent = Window.navigator.userAgent.toLowerCase ();
if (/(Msie|firefox|opera|chrome|netscape) d+ (d[d.) *)/.test (useragent)) {
BROWSER[REGEXP.$1] = true;
Browser.appname = regexp.$1;
Browser.version = regexp.$2;
else if (/versiond+ (d[d.) *). *safari/.test (useragent)) {//Safari
Browser.safari = true;
Browser.appname = ' Safari ';
Browser.version = regexp.$2;
}
return browser;
}
Call Example
var myos = AppInfo ();
If the current browser is IE, pop-up browser version, otherwise pop-up current browser name and version
if (Myos.msie) {
alert (myos.version);
} else {
Alert (Myos.appname + myos.version);
}
</script>

Cases

01

The code is as follows Copy Code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8″/>
<title> Untitled Document </title>
06
Modified <script type= "Text/javascript" >
The function Isie () {
Return Navigator.appName.indexOf ("Microsoft Internet Explorer")!=-1 && document.all;
10}
One function isIE6 () {
return Navigator.userAgent.split (";") [1].tolowercase (). IndexOf ("msie 6.0″) = =" -1″?false:true;
13}
The function isIE7 () {
return Navigator.userAgent.split (";") [1].tolowercase (). IndexOf ("msie 7.0″) = =" -1″?false:true;
16}
The function isIE8 () {
return Navigator.userAgent.split (";") [1].tolowercase (). IndexOf ("msie 8.0″) = =" -1″?false:true;
19}
function Isnn () {
Return navigator.userAgent.indexOf ("Netscape")!=-1;
22}
The function Isopera () {
Return navigator.appName.indexOf ("Opera")!=-1;
25}
-function isff () {
Return navigator.userAgent.indexOf ("Firefox")!=-1;
28}
The function ischrome () {
Return navigator.userAgent.indexOf ("Chrome") >-1;
31}
32
The function Showresult () {
if (Ischrome ()) {
Alert ("This is Google browser");
36}
37
if (Isie ()) {
Alert ("This is IE");
40}
41
The IF (IsIE6 ()) {
Alert ("This is Isie6″);"
44}
45
if (IsIE7 ()) {
Alert ("This is Isie7″);"
48}
49
if (IsIE8 ()) {
("This is Iisie8″);"
52}
53
The IF (ISNN ()) {
Alert ("This is Isnn");
56}
57
The IF (Isopera ()) {
Alert ("This is Isopera");
60}
61
if (ISFF ()) {
("This is ISFF");
64}
65}
</script>
67
68
70
<body >
<center><input type= "button" onclick= "Showresult ()" Name= "check" value= "detect" style= "width:200px; height:30px; " /></center>
</body>
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.