Helloworld program for reading data using Ajax XML
Source: Internet
Author: User
As the saying goes, it is easy to say that it is easy to do. It seems easy to compile on programming. Although there is no new technology in Ajax, it is just the transformation of ideas and the integration of old technologies, however, there are still a lot of problems when starting to work. First, I often use Firefox, and I immediately encountered the innertext incompatibility problem in Firefox. At first, I couldn't find the cause, later, it suddenly turned out to be a compatibility issue. Baidu, of course, should use the textcontent method in ff, and must follow the W3C standard to use getelementbyid to get the div, you cannot write ID directly in diagrams to save trouble. If you do it in IE, you won't know it in Firefox or other browsers. Follow the standard to make it readable.
Code :
Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<Head>
<Title> Ajax's first classic example: Hello World </title>
<SCRIPT type = "text/JavaScript">
VaR XMLHTTP;
Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}
Function startrequest (){
Createxmlhttprequest ();
Try {
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. Open ("get", "data. xml", true );
XMLHTTP. Send (null );
} Catch (exception ){
Alert ("The resource you want to access does not exist! ");
}
}
Function handlestatechange (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 | XMLHTTP. Status = 0 ){
// Retrieve the xml dom object
VaR xmldom = XMLHTTP. responsexml;
// Get the root of the XML document
VaR root = xmldom.doc umentelement;
Try
{
// Obtain the <info> result
VaR info = root. getelementsbytagname ('info ');
// Obtain the string
VaR str_data = info [0]. firstchild. Data;
// Change the content of the DIV and call the changetext function. Note that IE and FF are different.
Changetext (info [0]. firstchild. Data, 'showtext ');
// Innerhtml is the same
Document. getelementbyid ("showtexthtml"). innerhtml = '<strong>' + info [0]. firstchild. Data + '</strong> ';
} Catch (exception)
{
}
}
}
}
Function changetext (STR, element) {// compatible with IE and FF
If (navigator. appname. indexof ("Explorer")>-1 ){
Document. getelementbyid (element). innertext = STR;
} Else {
Document. getelementbyid (element). textcontent = STR;
}
}
</SCRIPT>
</Head>
<Body>
<Div>
<Input type = "button" value = "Return Ajax responsexml's value"
Onclick = "startrequest ();"/>
</Div>
<Div id = "showtext"> </div>
<Div id = "showtexthtml"> </div>
</Body>
</Html>
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.