日期:2014-05-16  浏览次数:20431 次

用纯javascript写的异步调用js文件xmlcommon.js二

function newXMLHttpRequest()
{
?var request=null;
?try
?{
??request=new ActiveXObject("Msxml2.XMLHTTP")
?}
?catch(e)
?{
??try
??{
???request=new ActiveXObject("Microsoft.XMLHTTP")
??}
??catch(sc)
??{
???request=null
??}
?}
?if(!request && typeof XMLHttpRequest!="undefined")
?{
??request=new XMLHttpRequest()
?}
?return request
}

function newXmlDocument()
{
?var xmlDoc=null;
?try
?{
??xmlDoc=new ActiveXObject("Msxml2.DOMDocument")
?}
?catch(e)
?{
??try
??{
???xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
??}
??catch(sc)
??{
???xmlDoc=null
??}
?}
?if(!xmlDoc && typeof XmlDocument!="undefined")
?{
??//XmlDocument doc = new XmlDocument();
??//doc.Load(strXmlName);
??xmlDoc=new XmlDocument()
?}
?return xmlDoc
}

function elmt( id )
{
?return document.getElementById(id);
}

function getReadyStateHandler(req, responseHandler)
{
?return function ()
?{
??if (req.readyState == 4)
??{
???if (req.status == 200)
???{
????responseHandler(req);
???}
???else
???{
????// 有HTTP问题发生
????alert("HTTP error: "+req.status);
???}
??}
?}
}

function getReadyStateHandlerText(req, targetId)
{
?return function ()
?{
??if (req.readyState == 4)
??{
???if (req.status == 200)
???{
????setElementValue(req, targetId);
???}
???else
???{
????// 有HTTP问题发生
????alert("HTTP error: "+req.statusText);
???}
??}
?}
}

function handlerRequestText( elementId )
{
?var jsp_name = elmt('jsp_name');
?var req = newXMLHttpRequest();
? ?var handler = getReadyStateHandlerText(req, elementId);
? ?req.onreadystatechange = handler;
? ?req.open("POST", jsp_name.value, true);
? ?req.send("");
}

function handlerRequestURLText( url, elementId )
{
?if( url.length == 0 )
?{
??clearAndHiddenElement( elementId );
??return;
?}
?else
?{
??var req = newXMLHttpRequest();
?? ?var handler = getReadyStateHandlerText(req, elementId);
?? ?req.onreadystatechange = handler;
?? ?req.open("POST", url, true);
?? ?req.send("");
? ?}
}

function handlerSynRequestXML( sendurl, urlparam )
{
?if( sendurl.length == 0 )
?{
??return null;
?}
?else
?{
??if( urlparam == null || urlparam == "undefined" )
??{
???urlparam = "";
??}
??var req = newXMLHttpRequest();
?? ?req.open("POST", sendurl, false);
?? ?req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
?? ?req.send(urlparam);
?? ?var xmldoc = req.responseXML;
?? ?setElementByXmlDoc( xmldoc );
? ?}
}

function handlerSynRequestTXT( sendurl, urlparam )
{
?if( sendurl.length == 0 )
?{
??return "";
?}
?else
?{
??if( urlparam == null || urlparam == "undefined" )
??{
???urlparam = "";
??}
??var req = newXMLHttpRequest();
?? ?req.open("POST", sendurl, false);
?? ?req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
?? ?req.send(urlparam);
?? ?var respTxt = req.responseText;
?? ?return respTxt;
? ?}
}

function setElementValue( request, elementId )
{
?document.getElementById(elementId).style.display="";
?document.getElementById(elementId).innerHTML=request.responseText;
}

function clearAndHiddenElement( elementId )
{
?document.getElementById(elementId).innerHTML="";
?document.getElementById(elementId).style.display="none";
}

function handlerRequestURLXML( sendurl, responseHandler, urlparam )
{
?if( sendurl.length == 0 )
?{
??responseHandler();
??return;
?}
?else
?{
??if( urlparam == "undefined" || urlparam == null )
??{
???urlparam = "