Ajax Combat: XmlDocument and XMLHttpRequest objects

Source: Internet
Author: User
Tags versions

As we've just seen, we can use the IFRAME to request data in the background, but this is essentially just a hack-style temporary solution. The intention of initially introducing an IFRAME is to display the visible content on the page, a use that distorts this intent. In a newer version of the popular web browser, objects specifically designed for asynchronous data transfer are introduced, and we'll see that it's much more convenient than an IFRAME.

XmlDocument and Xmlhttpresuest objects are not standard extensions of DOM in Web browsers, they just happen to be supported by most browsers. Their design goals are clear, that is, to get data later, which makes the business of making asynchronous calls very fluent. All two objects are private ActiveX components from Microsoft and can be accessed as JavaScript objects in IE browsers. Other browsers implement their native objects in accordance with similar functions and API calls. Two objects perform similar functions, but XMLHttpRequest can control requests more subtly. In this book, we will mainly use XMLHttpRequest, here we briefly introduce XmlDocument, so that you know what the object and XMLHttpRequest are different. Code Listing 2-8 shows a simple code to create a XmlDocument object.

Code Listing 2-8 GetXmlDocument () function

In most modern browsers, this function returns a XmlDocument object with the same API, although the way in which documents are created differs greatly from one browser to another.

This code checks whether the document object supports the implementation attribute required to create a native XmlDocument object (this property is found in the most recent Mozilla and Safari browsers). If it is not found, it will test whether the browser supports ActiveX objects (only Microsoft browsers can support it), and if so, it will try to locate an appropriate object. This script takes precedence over the newer version of the MSXML library.

It is a common practice to check your browser's vendor and version number information and use this information to develop branch codes for different browsers. In our view, this approach can easily lead to errors because it cannot anticipate future versions of the browser and will also exclude browsers that are capable of executing the script. In the GetXmlDocument () function, we do not try to guess the version of the browser, but directly check that the specific object is available. This method, also known as Object detection, makes it easier to support future versions of browsers, as well as detection browsers that we have not specifically tested, usually making the code more robust.

Code Listing 2-9 uses the same method as the previous code to get the XMLHttpRequest object, but slightly simpler.

Code Listing 2-9 Getxmlhttprequest () function

Similarly, we use object detection to test whether the native XMLHttpRequest object is supported, and, if not, to test whether the ActiveX object is supported. In browsers that do not support both, we simply return NULL. How do you deal with failure more gracefully? This question is left to the 6th chapter to be discussed in more detail.

We have created the object to send the request to the server, what are we going to do with it now?

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.