HelloWorld programs that use AJAX XML data to read _ajax related

Source: Internet
Author: User
Well, as the saying goes, easier said than done, put on the programming is easy to make up the difficulty, although there is no new technology Ajax, but the transformation of ideas and the integration of old technology, but to move hands or problems, the first is that I often use the Firefox browser, Immediately met the innertext in Firefox is not compatible with the problem, just start to find the reason, then suddenly reacted to the problem may be compatible, Baidu, sure enough, in the FF to use the Textcontent method, and must comply with the standard of the getElementById to obtain Div, can not map directly write ID, ie inside the line, in Firefox and other browsers do not know, or according to the standard to come, this also has readability well ^_^
Code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<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 = = | | xmlhttp.status = 0) {
Get the DOM object for XML
var xmldom = Xmlhttp.responsexml;
Get the root of an XML document
var root = xmldom.documentelement;
Try
{
Get <info> Results
var info = root.getelementsbytagname (' info ');
Fetch string
var str_data = Info[0].firstchild.data;
Change the content of the Div, call the Changetext function, note that IE and FF are not the same
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>
<body>
<div>
<input type= "button" value= "return ajax Responsexml ' s value"
Onclick= "Startrequest ();"/>
</div>
<div id= "Showtext" ></div>
<div id= "showtexthtml" ></div>
</body>
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.