An Ajax example

Source: Internet
Author: User

HTML:

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
<SCRIPT type = "text/JavaScript" src = "XMLHttpRequest. js"> </SCRIPT>
</Head>

<Body>
<Div id = "see">

</Div>
<Input type = "button" onclick = "sendrequest ()" value = "sendrequest">
<SCRIPT>

VaR XMLHTTP = new XMLHttpRequest ();
VaR xmldoc;
Function sendrequest (){
Document. All ("see"). innerhtml = "loading ....";
VaR url = "http: // localhost: 8080/tree. xml ";
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = viewpage;
XMLHTTP. Send (null );
}
Function viewpage (){

If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 ){
Alert ("Haha get XML ")
Xmldoc = XMLHTTP. responsexml;
VaR xmlobj = xmldoc.doc umentelement;
VaR STR = "";
For (VAR I = 0; I <xmlobj. childnodes. length; I ++ ){
VaR node = xmlobj. childnodes [I];
VaR text = node. getattribute ("nodeid ");
VaR value = text;
STR + = "<span style = 'display: Block' onclick = 'getsub (\" "+ value + "\") '> "+ TEXT +" </span> <br> <Div style = 'display: none' id = "+ TEXT +"> </div> ";
}
Document. all. See. innerhtml = STR;
// Alert (xmlobj. tagname)
}
}

Function getsub (ID ){
If (xmldoc! = NULL ){
VaR root = xmldoc. getelementsbytagname ("treenode ");
VaR str2 = "";
For (var k = 0; k <root. length; k ++ ){
If (root [K]. getattribute ("nodeid") = ID ){
Document. All (ID). style. Display = '';
Document. All (ID). innerhtml = "loading ..."
Alert ("Get sub Tree ")
VaR nodes = root [K]. childnodes;
For (VAR m = 0; m <nodes. length; m ++ ){
VaR node = nodes [m];
VaR text = node. getattribute ("nodeid ");
Str2 + = "<span onclick = 'getsub (\" "+ TEXT +" \ ") '> & nbsp; "+ TEXT +" </span> <br> <Div style = 'display: none' id = "+ TEXT +"> </div> ";
}
Document. All (ID). innerhtml = str2; // alert (document. All (ID). innerhtml)
}

}
} Else {
Alert ("Exception !! ");
}
}

</SCRIPT>
</Body>
</Html>
XML to be accessed:










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.