JavaScript window. ActiveXObject objects, how to distinguish the browser

Source: Internet
Author: User
Window. ActiveXObject) to determine whether the browser supports ActiveX controls, and if you support ActiveX controls, we can use Varxml=newactivexobject ("Microsoft.XMLHTTP") To create the XMLHttpRequest object (this is in a previous version of IE7). In newer IE versions, XMLHttpRequest objects can be created using the form varxml=newactivexobject ("Msxml2.xmlhttp"), while varxml= can be used in IE7 and non-IE browsers Newxmlhttprequest () creates the XMLHttpRequest object. So we have to consider the compatibility of browsers when creating this object.

Then there's the difference between XMLHttpRequest.

Let's take a look here to see how it is used. Before sending a request and processing a response using the XMLHttpRequest object, we must create a XMLHttpRequest object with JavaScript. (ie implements XMLHttpRequest as an ActiveX object, and other browsers [such as Firefox/safari/opear] implement it as a local JavaScript object). Let's take a look at how to use JavaScript specifically to create it:

<script language= "javascript" type= "Text/javascript" >
<!--
var xmlhttp; Creating XMLHttpRequest Objects
function Createxmlhttprequest () {
if (window. ActiveXObject) {//Determine if ActiveX controls are supported
XMLHTTP = new Activeobject ("Microsoft.XMLHTTP"); To create a XMLHttpRequest object by instantiating a new instance of ActiveXObject
}
else if (window. XMLHttpRequest) {//To determine whether to implement XMLHttpRequest as a local JavaScript object
XMLHTTP = new XMLHttpRequest (); Create an instance of the XMLHttpRequest (local JavaScript object)
}
}
-->
</script>

======================================================================

Use JS to distinguish between IE and other browsers, as well as using JS to distinguish between ie6-8 methods.
1, document.all
2 、!! Window. ActiveXObject;

Use the following methods:
if (document.all) {
Alert ("IE browser");
}else{
Alert ("Non IE browser");
}

if (!! Window. ActiveXObject) {
Alert ("IE browser");
}else{
Alert ("Non IE browser");
}

Here are the ways to differentiate between IE6, IE7, and IE8:
var isie=!! Window. ActiveXObject;
Varisie6=isie&&!window. XMLHttpRequest;
varisie8=isie&&!! Document.documentmode;
varisie7=isie&&!isie6&&!isie8;
if (Isie) {
if (isIE6) {
Alert ("Ie6″");
}else if (isIE8) {
Alert ("Ie8″");
}else if (isIE7) {
Alert ("Ie7″");
}
}

First of all we make sure that this browser is in the case of IE, conducted at a time of detection, if you have doubts about this, you can test.

I use this directly in judgment, and you can also declare them as variables to use first. It is said that Firefox will also join document.all this way, so it is recommended that the second method should be safer.

=============================================================================

Do you know how many kinds of browsers there are in the world? In addition to our well-known IE, Firefox, Opera,safari Four browsers, there are nearly hundreds of browsers in the world.

A few days ago, the browser family had just born a little prince, the Chrome browser that Google launched. Because of the blue-blooded chrome, no one dared to underestimate him, even though he was still a little boy. Later, we often say that the "four great talent" of the browser has to be renamed as "Five Golden flowers."

In the front-end development of the Web site, browser compatibility problem has let us rush, the birth of Chrome did not know to give us a lot of trouble. Browser compatibility is the first problem to be solved by the front-end development framework, in order to solve the compatibility problem, we must first accurately determine the browser type and its version.

JavaScript is the primary language for front-end development, and we can write JavaScript programs to determine the type and version of the browser. JavaScript can determine browser types in general there are two ways, one is based on a variety of browser-specific properties to distinguish, the other is by analyzing the browser's useragent properties to judge. In many cases, after the value is judged by the browser type, it is also necessary to judge the browser version to handle the compatibility issue, and to judge the browser version is generally only through the analysis of the browser useragent to know.

Let's first analyze the features and useragent of various browsers.

IE

Only IE supports the creation of 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/2008070208firefox/3.0.1
mozilla/5.0 (Windows; U Windows NT 5.1) gecko/20070309firefox/2.0.0.3
mozilla/5.0 (Windows; U Windows NT 5.1) gecko/20070803firefox/1.5.0.12
Where 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-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 close to opera's number.

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; 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

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; U Windows NT 5.2) applewebkit/525.13 (khtml,like Gecko) chrome/0.2.149.27 safari/525.13

Where the version number is in Chrome only after the number.

Interestingly, Chrome's useragent also includes Safari features, which is perhaps the basis for all of the apps that Chrome can run on Apple's browser.

As soon as we know the above information, we can base these features on the browser type and its version. We will keep the result of the judgment in the Sys namespace, as the basic information of the front-end framework, for future programs to read. If you judge a browser, the Sys namespace will have a property of that browser name, and its value is the version number of the browser. For example, if IE7.0 is judged, the sys.ie value is 7.0, and if Firefox3.0 is judged, the Sys.firefox value is 3.0. Here is the code to judge 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];

The following tests
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>

We put the judgment of IE on the first, because IE users the most, followed by the judgement of Firefox. According to the number of users to determine the type of browser, you can improve the efficiency of judgment, less do not work hard. The reason we put Chrome on the third judgment is because we predict that Chrome will soon become the third-most-market-share browser. In the analysis of the browser version, a regular expression is used to extract the version information.

If your JavaScript is playing high, you can also write the previous judgment code like this:

< script type = "Text/javascript" >
var Sys = {};
var ua = Navigator.userAgent.toLowerCase ();
Window. ActiveXObject? sys.ie = Ua.match (/MSIE) ([\d.] +)/) [1]:
Document.getboxobjectfor? Sys.firefox = Ua.match (/firefox\/([\d.] +)/) [1]:
Window. Messageevent &&! Document.getboxobjectfor? Sys.chrome = Ua.match (/chrome\/([\d.] +)/) [1]:
Window.opera? Sys.opera = Ua.match (/opera. [\d.] +)/) [1]:
Window.opendatabase? Sys.safari = Ua.match (/version\/([\d.] +)/) [1]: 0;

The following tests
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 >

This will make the JavaScript code a little more streamlined. Of course, a little less readable, it depends on whether you pay attention to efficiency or maintainability.

Using different features to judge the browser's approach, although useragent is faster than the regular expression analysis, these features may change with the browser version. For example, a browser-specific feature has been successful in the marketplace, and other browsers may be able to join the feature, so that the browser's unique features disappear, resulting in our judgment failure. Therefore, the relative insurance approach is to determine the browser type by parsing the features in the useragent. Moreover, the judgment version information also needs to parse the browser the useragent.

By analyzing the useragent information of various browsers, it is not difficult to distinguish regular expressions of various browsers and their versions. Moreover, the judgment of the browser type and the judgement of the version can be integrated into one. 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.] +) / )) ? 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;

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.