Using the XMLHttpRequest object in a Mozilla-based browser

Source: Internet
Author: User
Tags bind documentation http request xml attribute

IE extends its functionality by allowing developers to use secure ActiveX components within a Web page. Microsoft's product in a stateless connection is the XMLHTTP component.

With the XMLHTTP component, developers can transmit data directly to or from the server without navigating from the current Web page. This feature is important because it helps reduce the pain of stateless connections. It also eliminates the need to download redundant HTML, which can increase the speed of the process.

Mozilla responds by creating its own inherited XML proxy class: the XMLHttpRequest class. For most cases, the XMLHttpRequest object behaves much like a XMLHTTP component. This means that you can use a lot of the same help information as the XMLHTTP component. Methods and properties are similar, however, all methods and properties begin with lowercase letters, and some properties are not supported.

Here is a simple example of using extensible Binding Language (XBL) to bind a behavior to a <INPUT> element to update an XML data island. Data is sent to the server for processing.

Xml_http.asp:

<%@ Language=vbscript%>
<%
Response.Expires =-1
If Request ("action") = "Update" Then
Dim XML
Set XML = Server.CreateObject ("MSXML2.") DOMDocument ")
Xml.load Request
Response.Writexml.xml
Set xml = Nothing
Response.End
End If
%>
<style>
. linkeddata {
-moz-binding:url (Link_data.xml#link);
}
</style>
<script language= "JavaScript" >
<!--
function Test () {
var xml = document.getElementById ("XmlData");
if (XML!= null) {
Varxmlhttp = new XMLHttpRequest ();
Xmlhttp.open ("POST", "Xml_http.asp?action=update", false);
Xmlhttp.send (xml.innerhtml);
alert (Xmlhttp.responsetext);
}
}
-->
</script>
<body>

<xml id= "XmlData" name= "XmlData" style= "Visibility:hidden"; >
<root>
<link_data/>
</root>
</xml>

<form id= "thisform" name= "Thisform" >

<input type= "text" class= "Linkeddata" name= "Txtlink" id= "Txtlink" size= ">"
<br>
<input type= "button" value= "Test" >

</form>

</body>

Link_data.xml:

<?xml version= "1.0"?>
<xbl:bindingsxmlns:xbl= "HTTP://WWW.MOZILLA.ORG/XBL">
<xbl:binding id= "link" >
<xbl:handlers>
<xbl:handler event= "Change" >
var xml = document.getElementById ("XmlData");
var root = xml.childnodes[1];
Varlink_data = root.childnodes[1];
Link_data.childnodes[0].nodevalue = Event.originalTarget.value;
</xbl:handler>
</xbl:handlers>
</xbl:binding>
</xbl:bindings>


Xml_http.asp file creates an XML data island, the data island created has an important node: Link_data. Also, this HTML representation creates a text box and a button to create an event that sends the XML data to the server.

IE allows you to embed XML data islands and use that data island to bind HTML elements to the underlying data. Mozilla relies on XBL to create behavior for each element. With XBL, you can create an action for the <INPUT> element, so the "bound" XML node can be updated when the onchange event occurs. The "Linkeddata" style class notifies the Mozilla file that Link_data.xml will be used to create bindings. This class is used in the <input type= "text" ... > elements.

If you analyze Link_data.xml code, you will notice that the OnChange event handler simply gets the Link_data node of the XML data island and then updates the value of the text node. This is a very simple approach to this solution; It updates the data only if the value in the text box changes. This is not part of the two-way binding mechanism, because if the value of the node changes, its changes are not reflected in the text box.

When the user clicks the test button, something strange happens. A new XMLHttpRequest object was created to improve XML data to the server. Then, just as the XMLHTTP component works in IE, you use the open () method to open the remote page. In this example, I added a query string to the xml_http.asp page so I could use the same page to process the XML transfer. Using the Send () method, I pass the innerhtml of the XMLData data island. (in IE, this is the same as the XML attribute that specifies the XMLData data island).

When the xml_http.asp page is loaded, it checks whether the "action" parameter is passed to the page and whether that parameter equals "Update". If it is-just when we make the XMLHttpRequest request-it creates a domdocument component, loads the XML from the request object, and then returns DOMDocument XML. The request object exposes the IStream interface, allowing the DOMDocument to load XML directly from the Request object. (In another language, you can use post data sent from an HTTP request.) )

This is a very simple example of Mozilla's XML capabilities. One of the drawbacks of manipulating XML in Mozilla is that the documentation is not good. As mentioned above, for most parts, you can use documentation for Microsoft's XMLHTTP component.



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.