Ajax Primer XMLHttpRequest _ajax Related

Source: Internet
Author: User
Before you can use the XMLHttpRequest object to send a request and process a response, you must first create a XMLHttpRequest object with JavaScript. Since XMLHttpRequest is not a standard for the consortium, there are several ways to use JavaScript to create instances of XMLHttpRequest. Internet Explorer implements XMLHttpRequest as an ActiveX object, and other browsers (such as Firefox, Safari, and Opera) implement it as a local JavaScript object. Because of these differences, JavaScript code must contain relevant logic to use ActiveX technology or use local JavaScript object technology to create an instance of XMLHttpRequest.

Many people may remember the previous days when the JavaScript and DOM implementations of different browsers were so varied that after listening to these words, these people might shudder again. Fortunately, in order to make it clear how to create an instance of the XMLHttpRequest object, it is not necessary to write code in such detail to distinguish the browser type. All you have to do is check to see if the browser provides support for ActiveX objects. If your browser supports ActiveX objects, you can use ActiveX to create XMLHttpRequest objects. Otherwise, the local JavaScript object technology will be used to create it. Listing 2-1 shows how simple it is to write Cross-browser JavaScript code to create a XMLHttpRequest object instance.

Code Listing 2-1  Create an instance of the XMLHttpRequest object

var xmlHttp;

function Createxmlhttprequest () {
if (window. ActiveXObject) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();
}
}

As you can see, creating XMLHttpRequest objects is fairly easy. First, you create a global scope variable XMLHTTP to hold the reference to this object. The Createxmlhttprequest method completes the specific work of creating a XMLHttpRequest instance. There is only a simple branching logic (selection logic) in this method to determine how to create an object. To window. A ActiveXObject call returns an object, or a null,if statement that returns the result of the call as True or False (False if the return object is true) to indicate whether the browser supports ActiveX controls. It is appropriate to know if the browser is Internet Explorer. If it does, create a XMLHttpRequest object by instantiating a new instance of ActiveXObject, and pass in a string indicating what type of ActiveX object to create. In this example, the string provided for the constructor is microsoft.xmlhttp, which means you want to create an instance of the XMLHttpRequest.

If window. ActiveXObject call fails (returns NULL), JavaScript will go to the Else statement branch to determine whether the browser implements XMLHttpRequest as a local JavaScript object. If there is a window.

XMLHttpRequest, an instance of XMLHttpRequest is created.

Because JavaScript has dynamic type attributes and XMLHttpRequest implementations on different browsers are compatible, you can access the properties and methods of the XMLHttpRequest instance in the same way, regardless of how the instance is created. This greatly simplifies the development process, and there is no need to write browser-specific logic in JavaScript.
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.