手机
当前位置:查字典教程网 >编程开发 >AJAX相关 >建立XMLHttpRequest对象
建立XMLHttpRequest对象
摘要:utl.js复制代码代码如下:functioncreateXmlTree(node,indent){if(node==null)return...

utl.js

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

if(node==null)

return"";

varstr="";

switch(node.nodeType){

case1://Element

str+="<divclass='element'><<spanclass='elementname'>"+node.nodeName+"</span>";

varattrs=node.attributes;

for(vari=0;i<attrs.length;i++)

str+=createXmlAttribute(attrs[i]);

if(!node.hasChildNodes())

returnstr+"/></div>";

str+="><br/>";

varcs=node.childNodes;

for(vari=0;i<cs.length;i++)

str+=createXmlTree(cs[i],indent+3);

str+="</<spanclass='elementname'>"+node.nodeName+"</span>></div>";

break;

case9://Document

varcs=node.childNodes;

for(vari=0;i<cs.length;i++)

str+=createXmlTree(cs[i],indent);

break;

case3://Text

if(!/^s*$/.test(node.nodeValue))

str+="<spanclass='text'>"+node.nodeValue+"</span><br/>";

break;

case7://ProcessInstruction

str+="<?"+node.nodeName;

varattrs=node.attributes;

for(vari=0;i<attrs.length;i++)

str+=createXmlAttribute(attrs[i]);

str+="?><br/>"

break;

case4://CDATA

str="<divclass='cdata'><![CDATA[<spanclass='cdata-content'>"+

node.nodeValue+

"</span>]"+"]></div>";

break;

case8://Comment

str="<divclass='comment'><></div>";

break;

case10:

str="<divclass='doctype'><!DOCTYPE"+node.name;

if(node.publicId){

str+="PUBLIC""+node.publicId+""";

if(node.systemId)

str+="""+node.systemId+""";

}

elseif(node.systemId){

str+="SYSTEM""+node.systemId+""";

}

str+="></div>";

//TODO:HandlecustomDOCTYPEdeclarations(ELEMENT,ATTRIBUTE,ENTITY)

break;

default:

//alert(node.nodeType+"n"+node.nodeValue);

inspect(node);

}

returnstr;

}

functioninspect(obj){

varstr="";

for(varkinobj)

str+="obj."+k+"="+obj[k]+"n";

window.alert(str);

}

functioncreateXmlAttribute(a){

return"<spanclass='attribname'>"+a.nodeName+"</span><spanclass='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>" + 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 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

}

【建立XMLHttpRequest对象】相关文章:

Ajax创建XMLHttp对象的完美兼容性代码

使用Ajax技术通过XMLHttpRequest对象完成首页登录功能

AJAX入门之XMLHttpRequest慨述

建一个XMLHttpRequest对象池

AJAX客户端说明,XMLHttpRequest对象

XMLHttpRequest of ajax

AJAX在不同浏览器中XMLHttpRequest对象的生成示例

javascript对XMLHttpRequest异步请求的面向对象封装

Ajax初试之读取数据篇

[js]轻便的XMLHttpRequest应用函数:downloadUrl()

精品推荐
分类导航