欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

建立XMLHttpRequest对象

程序员文章站 2022-06-24 16:17:45
utl.js复制代码 代码如下:function createxmltree(node, indent) {   &nbs...

utl.js

复制代码 代码如下:
function createxmltree(node, indent) {

    if (node == null)
        return "";
    var str = "";

    switch (node.nodetype) {
        case 1:    // element
            str += "<div class='element'><<span class='elementname'>" + node.nodename + "</span>";

            var attrs = node.attributes;
            for (var i = 0; i < attrs.length; i++)
                str += createxmlattribute(attrs[i]);

            if (!node.haschildnodes())
                return str + "/></div>";

            str += "><br />";

            var cs = node.childnodes;
            for (var i = 0; i < cs.length; i++)
                str += createxmltree(cs[i], indent + 3);

            str += "</<span class='elementname'>" + node.nodename + "</span>></div>";
            break;

        case 9:    // document
            var cs = node.childnodes;
            for (var i = 0; i < cs.length; i++)
                str += createxmltree(cs[i], indent);
            break;

        case 3:    // text
            if (!/^\s*$/.test(node.nodevalue))
                str += "<span class='text'>" + node.nodevalue + "</span><br />";
            break;

        case 7:    // processinstruction
            str += "<?" + node.nodename;

            var attrs = node.attributes;
            for (var i = 0; i < attrs.length; i++)
                str += createxmlattribute(attrs[i]);

            str+= "?><br />"
            break;

        case 4:    // cdata
            str = "<div class='cdata'><![cdata[<span class='cdata-content'>" + 
                node.nodevalue +
            "</span>]" + "]></div>";
            break;

        case 8:    // comment
            str = "<div class='comment'><!--<span class='comment-content'>" + 
                node.nodevalue +
            "</span>--></div>";
            break;

        case 10:
                str = "<div class='doctype'><!doctype " + node.name;
                if (node.publicid) {
                    str += " public \"" + node.publicid + "\"";
                    if (node.systemid) 
                        str += " \"" + node.systemid + "\"";
                }
                else if (node.systemid) {
                    str += " system \"" + node.systemid + "\"";
                }
                str += "></div>";

                // todo: handle custom doctype declarations (element, attribute, entity)

                break;

        default:
            //alert(node.nodetype + "\n" + node.nodevalue);
            inspect(node);
    }

    return str;
}

function inspect(obj) {
    var str = "";
    for (var k in obj)
        str += "obj." + k + " = " + obj[k] + "\n";
    window.alert(str);
}

function createxmlattribute(a) {
    return " <span class='attribname'>" + a.nodename + "</span><span class='attribvalue'>=\"" + a.nodevalue + "\"</span>";
}

复制代码 代码如下:

<html><head><title>ajax test</title>
<meta http-equiv=content-type content="text/html; charset=gb2312">
<script src="ajax test_files/utl.js"></script>
<link rev=stylesheet media=all href="ajax test_files/ajax.css" type=text/css
rel=stylesheet>
<script>
//------------ xmlhttpobj类 ----------------
function xmlhttpobject(url,syne){
 var xmlhttp=null
 var o=this
 this.url=url
 this.syne=syne
 this.senddata = function()
  {
    if (window.xmlhttprequest) {
       xmlhttp = new xmlhttprequest();
    } else if (window.activexobject) {
       xmlhttp = new activexobject("microsoft.xmlhttp");
    }
    with(xmlhttp){
     open("get", this.url, this.syne);
     onreadystatechange = o.callback;
     send(null);
    }
  }

 this.callback=function()
  {
    if (xmlhttp.readystate == 4) {
        if (xmlhttp.status == 200) {
          o.debugxml("readystate:" + xmlhttp.readystate + "<br/>")
          o.debugxml("status :" + xmlhttp.status + "<br/>")
          o.debugxml(" <div style='background:#ffc;margin-left:10px;border:1px solid #669;padding:3px'>" + createxmltree(xmlhttp.responsexml,0) + "</div>")
        }
    }

  }

 this.gettext=function()
  {
   if (xmlhttp==null) {return "还没加载 xmlhttprequest"}
   if (xmlhttp.readystate==4) {return xmlhttp.responsetext}
   return xmlhttp.readystate
  }

 this.debugxml=function(log)
  {
   try{document.getelementbyid("xmldebug").innerhtml+=log}
   catch(e){}
  }
}
var xmldoc1=new xmlhttpobject("tree.xml",true)

</script>

<meta content="mshtml 6.00.3790.2706" name=generator></head>
<body><select
onchange='xmldoc1.url=this.value;document.getelementbyid("xmldebug").innerhtml=""'>
  <option value=tree.xml selected>tree.xml</option> <option
  value=xtree.xsl>xtree.xsl</option> <option value=/feed.asp>blog
feed</option></select> <input onclick=xmldoc1.senddata() type=button value=xmldoc1.senddata()> <input onclick=alert(xmldoc1.gettext()) type=button value=xmldoc1.gettext()> <input onclick="if (confirm('clear log?')) {document.getelementbyid('xmldebug').innerhtml=''}" type=button value=clearlog>

<div id=xmldebug></div></body></html>


复制代码 代码如下:

html {
 border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px
}
body {
 border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px
}
body {
 background: buttonface; font: message-box
}
form {
 padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px
}
fieldset fieldset {
 margin: 5px
}
button {
 margin-left: 5px
}
textarea {
 width: 100%; height: 200px
}
#out {
 border-right: threeddarkshadow 1px solid; padding-right: 3px; border-top: threeddarkshadow 1px solid; margin-top: 5px; padding-left: 3px; background: white; padding-bottom: 3px; border-left: threeddarkshadow 1px solid; color: black; padding-top: 3px; border-bottom: threeddarkshadow 1px solid; font-family: verdana
}
#srctextcontainer {
 display: none
}
.element {
 padding-left: 16px; color: blue
}
.elementname {
 color: darkred
}
.attribname {
 color: red
}
.attribvalue {
 color: blue
}
.text {
 padding-left: 16px; color: windowtext
}
.cdata-content {
 display: block; padding-left: 16px; border-left: gray 1px solid; color: windowtext; font-family: courier new; white-space: pre
}
.comment {
 padding-left: 16px; color: blue
}
.doctype {
 padding-left: 16px; color: blue
}
.comment-content {
 display: block; color: gray; font-family: courier new; white-space: pre
}
#xmldebug {
 border-right: #000 1px solid; padding-right: 4px; border-top: #000 1px solid; margin-top: 6px; padding-left: 4px; font-size: 12px; background: #fff; padding-bottom: 4px; overflow: auto; border-left: #000 1px solid; padding-top: 4px; border-bottom: #000 1px solid; height: 400px
}