How to create a highly compatible XMLHttpRequest object in Ajax

Source: Internet
Author: User
XMLHttpRequest is the core of Ajax applications and may be unfamiliar to many readers. Let's start from here. XMLHttpRequest is the core of Ajax applicationsAnd it may be unfamiliar to many readers. Let's start from here.
Do you still remember the nasty Browser Wars a few years ago? Nothing returns the same result on different browsers. Whether you believe it or not, these wars are still going on, although the scale is small. But it is strange that XMLHttpRequest has become one of the victims of this war. . Therefore, you may need to use different methods to obtain the XMLHttpRequest object.I will explain it in detail below.
Use the Microsoft browser
Microsoft browser Internet Explorer uses the MSXML parser to process XML (for more information about MSXML, see references ). Therefore, if an Ajax application needs to work with Internet Explorer, an object must be created in a special way. But it is not that simple. Depending on the version of JavaScript technology installed in Internet Explorer, MSXML actually has two different versions. Therefore, you must write code for these two cases. See listing 3. The Code creates an XMLHttpRequest on the Microsoft browser.
Listing 3. Create an XMLHttpRequest object in the Microsoft browserVar xmlHttp = false; try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {try {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}catch (e2) {xmlHttp = false ;}}
You may not fully understand the code, but it does not matter. At the end of this series of articles, you will have a better understanding of JavaScript programming, error handling, and Conditional compilation. Now you only need to remember the two lines of code: XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");And XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");.
These two lines of code basically try to create an object using a version of MSXML. If the Code fails, use another version to create the object. Good, right? If none of them are successful, set the xmlHttp variable to false to tell you that the Code has a problem. If this happens, it may be because a non-Microsoft browser is installed and different code is required.
Processing Mozilla and non-Microsoft browsers
If the selected browser is not Internet Explorer, or you want to write code for a non-Microsoft browser, you need to use different codes. In fact, it is a simple line of code shown in Listing 1:
Var xmlHttp = new XMLHttpRequest object ;.
This line of much simpler Code creates XMLHttpRequest objects in Mozilla, Firefox, Safari, Opera, and basically all non-Microsoft browsers that support Ajax in any form or method.
Combined
The key is to support all browsers. Who wants to write an application that can only be used in Internet Explorer or non-Microsoft browsers? Or worse, write an application twice? Of course not! Therefore, the Code must support both Internet Explorer and non-Microsoft browsers. Listing 4 shows the code.
Listing 4. Creating XMLHttpRequest objects in multiple browsers/* Create a new XMLHttpRequest object to talk to the Web server */var xmlHttp = false;/* @ cc_on @ * // * @ if (@ _ jscript_version> = 5) try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {try {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");} catch (e2) {xmlHttp = false; }}@ end @ */if (! XmlHttp & typeof XMLHttpRequest! = 'Undefined') {xmlHttp = new XMLHttpRequest ();}
Now, regardless of the comment-out strange symbols such as @ cc_on, this is a special JavaScript compiler command and will be discussed in detail in the next article on XMLHttpRequest. . The core of this Code is divided into three steps:1. Create a variable xmlHttp to reference the XMLHttpRequest object to be created. 2. Try to create this object in Microsoft browser: o try to create it using the Msxml2.XMLHTTP object. O if it fails, try the Microsoft. XMLHTTP object again. 3. If xmlHttp is still not created, this object is created in a non-Microsoft manner. Finally, xmlHttp should reference a valid XMLHttpRequest object, regardless of the browser that runs.
Security
How is security? Now the Browser allows users to increase their security levels, disable JavaScript technology, and disable any options in the browser. In this case, the Code does not work anyway. At this time, the problem must be properly handled. This should be discussed in a separate article. It should be put into the future (is this series long enough? Don't worry, you may have mastered it before reading it ). Now we need to write a robust but not perfect code, which is good for mastering Ajax. We will discuss more details later.
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.