Cross-browser Development Experience Summary (6) -- XML Document Processing & XMLHTTPRequest object

Source: Internet
Author: User
Document directory
  • Load XML files
  • Load XML strings
  • Selectnodes (), selectsinglenode () method
  • XML attributes
  • Obtain the XMLHTTPRequest object in IE and other browsers.
  • Description of open method parameters
  • Set the default value of the send method parameter
XML document processing and loading XML files

IE:

Xmldoc = new activexobject ("Microsoft. xmldom ");

Xmldoc. async = "false ";

Xmldoc. Load ("books. xml ");

Other browsers:

Xmldoc = Document. Implementation. createdocument ("", "", null );

Xmldoc. async = "false ";

Xmldoc. Load ("books. xml ");

 

Load XML strings

IE:

Xmldoc = new activexobject ("Microsoft. xmldom ");

Xmldoc. async = "false ";

Xmldoc. loadxml (txt );

Other browsers:

Parser = new domparser ();

Xmldoc = parser. parsefromstring (txt, "text/XML ");

Selectnodes (), selectsinglenode () method

These two methods are only available in IE. If you want to use a cross-browser method, try to use the standard DOM method instead, or implement the above methods before using them in other browsers.

XML attributes

Ie can use obj. XML to display the node content of OBJ, but this attribute is not available in other browsers. Therefore, you must use (New xmlserializer (). serializetostring (OBJ) to display the node content.

 

The XMLHTTPRequest object obtains the XMLHTTPRequest object in IE and other browsers.

Obtain the XMLHTTPRequest object in IE and other browsers.

Function getxmlhttprequest ()

{

VaR myrequest = NULL;

If (window. XMLHttpRequest)// IEOther browsers

{

Myrequest = new XMLHttpRequest ();

}

Else if (typeof activexobject! = "Undefined ")// IE

{

Myrequest = new activexobject ("Microsoft. XMLHTTP ");

}

Return myrequest;

}

Then we can use the above method for request operations.

VaR myrequest = getxmlhttprequest ();

XMLHTTP. onreadystatechange = statechange;

XMLHTTP. Open ("get", URL, true );

XMLHTTP. Send (null );

 

Description of open method parameters

XMLHTTP. the first parameter in open ("get", URL, true) indicates the request method, "Post" or "get". The second parameter is the request sending Address; the third parameter indicates whether the request is made asynchronously. If an asynchronous request is used, the browser will continue to execute the post-send statement while waiting for the response from the request address after sending the request. After receiving the response, the browser will execute XMLHTTP. the onreadystatechange callback method is set to statechange. Therefore, you need to write the subsequent operations after the response in this callback method.

It should be noted that if the request is sent synchronously, the browser will execute the post-send statement after the request response. Therefore, it is best to write the post-response operation directly after the send statement, it is not written in XMLHTTP as an asynchronous request. in the callback method set by onreadystatechange, there are some inconsistencies in whether the browsers still execute this callback method after synchronizing requests: for example, if firefox3.0 does not trigger the onreadystatechange event after sending a synchronous request to obtain a response, it will naturally not execute this callback method.

Set the default value of the send method parameter

Although the XMLHttpRequest APIs of different browsers are the same, the methods and attributes for calling XMLHttpRequest in different browsers are different. For example, the send method does not include parameters in IE, for example, myxmlhttprequest. send (); can still work normally, but in Firefox, a parameter must be passed for the send method, that is, in Firefox, The send method parameter has no default value and must be assigned a value, even null.

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.