Files
2025-09-12 15:53:10 +08:00

414 lines
8.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//XML Request 返回同一处理。
//@req newXMLHttpRequest函数实例
//@responseXmlHandler 成功后的回调函数
//@exceptionHandler 请求失败处理的回调函数。如果不指定则默认用alert处理
function getReadyStateHandler(req, responseXmlHandler, exceptionHandler)
{
return function ()
{
if (req.readyState == 4)// 如果请求的状态是“完成”
{
var message = null;
if (req.status == 200)
{
var xml = req.responseXML;
if (xml == null)
{
message = "返回的不是合法的XML串" + req.responseText;
}
else
{
var redirect = xml.getElementsByTagName("redirect");
if (redirect && redirect.length)
{
window.location.href = redirect[0].getAttribute("href");
return;
}
var result = xml.getElementsByTagName("result")[0];
if (result == null || "true" == result.getAttribute("status"))
{
responseXmlHandler(result, req);
return;
}
if (result)
{
message = result.getAttribute("exception");
}
}
}
else if (req.status == 12029 || req.status == 0)
{
message = "网络不通";
}
else if (req.status == 12030)
{
message = "后台服务程序内部错误,请刷新重试";
}
else if (req.status == 404)
{
message = "请求页面不存在" + req.status;
}
else if (req.status == 500)
{
message = "后台服务程序内部出错";
}
else
{
message = "非预知错误:" + req.status;
}
if(exceptionHandler)
{
exceptionHandler(message, req.responseText);
}
else
{
finishProcess();
if (message == null)
{
message = req.responseText;
}
alert(message);
}
}
}
}
//用于新浏览器的XML HttpRequest
function newXMLHttpRequest()
{
if (window.XMLHttpRequest) return new XMLHttpRequest();
else return new ActiveXObject("Msxml2.XMLHTTP");
}
function convertParameter(value)
{
if (value.indexOf("%") >= 0)
value = value.replace(/\%/g, "%25");
if (value.indexOf("&") >= 0)
value = value.replace(/&/g, "%26");
if (value.indexOf("=") >= 0)
value = value.replace(/=/g, "%3d");
if (value.indexOf(" ") >= 0)
value = value.replace(/\ /g, "%20");
if (value.indexOf("?") >= 0)
value = value.replace(/\?/g, "%3f");
return value;
}
/**
* 提交异步XMLRequest请求。
* @param postPage 请求页面地址
* @param postArgumentArray 请求页面传递的参数名机器参数值构成的数组。
* 如 参数1名称参数1的值参数2名称参数2的值...[参数n名称参数n的值]
* @param responseXmlHandler 请求成功后回调的函数名称
* @param exceptionHandler 如果请求过程出现错误时,错误处理函数名称
*/
function postRequest(postPage, postArgumentArray, responseXmlHandler, exceptionHandler)
{
try
{
if (!prepareProcessWindow("postProgress"))
{
return;
}
var req = newXMLHttpRequest();
var exceptionHandle = exception;
if (exceptionHandler && typeof(exceptionHandler) == "function")
{
exceptionHandle = exceptionHandler;
}
//alert(exceptionHandle);
var handlerFunction = getReadyStateHandler(req, responseXmlHandler, exceptionHandle);
req.onreadystatechange = handlerFunction;
req.open("POST", postPage, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var postArgumentList = "";
for (var i = 0; i < postArgumentArray.length/2; i++)
{
var name = convertParameter(postArgumentArray[i*2]);
var value = convertParameter(postArgumentArray[i*2 + 1]);
if (i > 0) postArgumentList += "&";
postArgumentList += name + "=" + value;
}
req.send(postArgumentList);
postArgumentList = null;
setTimeout('showProgress("正在处理请求......")', 2000);
}
catch(e)
{
finishProcess();
exceptionHandle("程序错误:" + e.name + "" + e.message);
}
}
function exception(info, detail)
{
finishProcess();
if (info) alert(info);
else if (detail) alert(detail);
}
//判断参数对象是否为对象数组
function isArray(a)
{
if(a && a.length
&& Object.prototype.toString.apply(a).indexOf('[object ') >= 0)
{
return true;
}
else
{
return false;
}
}
//获得form中某个input对象当前的输入值
function getFieldValue(a)
{
if (isArray(a))
{
for(var i = 0; i < a.length; i++)
{
if (a[i].checked)
{
return a[i].value;
}
}
return "";
}
else if (a && a.value)
{
return a.value;
}
else
{
return "";
}
}
function focusInput(x)
{
try
{
if (x)
{
if(isArray(x) && x[0])
{
if (x[0].select) x[0].select();
x[0].focus();
}
else
{
if (x.select) x.select();
x.focus();
}
}
}
catch(e)
{
//alert(e);
}
}
function getElements(parentNode, subNodeTagName)
{//getElementsByTagName在IE/Chrome流览器中才支持
return parentNode.getElementsByTagName(subNodeTagName);
}
function getElementAt(parentNode, subNodeTagName, index)
{
return getElements(parentNode, subNodeTagName)[index];
}
var msg = new Object();
msg.busyMark = false;
msg.createDialog=function(id, isModal)
{
var dialog = document.createElement("div");
dialog.className='dialog';
dialog.id=id;
var titleBar=document.createElement("div");
titleBar.className='titleBar';
var closeButton=document.createElement("span");
closeButton.dialog=dialog;
closeButton.onclick=msg.hideDialog;
closeButton.className='titleButton';
var closeTxt=document.createTextNode("X");
closeButton.appendChild(closeTxt);
titleBar.appendChild(closeButton);
dialog.appendChild(titleBar);
var tbl=document.createElement("table");
dialog.appendChild(tbl);
dialog.tbod=document.createElement("tbody");
tbl.appendChild(dialog.tbod);
if (isModal)
{
dialog.modalLayer=document.createElement("div");
dialog.modalLayer.className='modal';
dialog.modalLayer.style.display='none';
dialog.modalLayer.appendChild(dialog);
document.body.appendChild(dialog.modalLayer);
}
else
{
dialog.className+= ' non-modal';
dialog.style.diaplay='none';
document.body.appendChild(dialog);
}
return dialog;
}
msg.hideDialog=function(e)
{
var dialog=this.dialog?this.dialog:msg.dialog;
if (dialog)
{
if (dialog.modalLayer)
{
dialog.modalLayer.style.display='none';
}
else
{
dialog.style.diaplay='none';
}
}
}
msg.showDialog=function(e)
{
var dialog=this.dialog?this.dialog:msg.dialog;
if (dialog)
{
if (dialog.modalLayer)
{
dialog.modalLayer.style.display='block';
}
else
{
dialog.style.diaplay='block';
}
}
}
msg.showMessage=function(message, el)
{
if (!el) el = msg.dialog.tbod;
var inTable=(el.tagName=="TBODY");
var topEl = null;
var row = document.createElement("tr");
if (!inTable)
{
topEl=document.createElement("table");
var tbod=document.createElement("tbody");
topEl.appendChild(tbod);
tbod.appendChild(row);
}
else
{
topEl = row;
}
var txtTd=document.createElement("td");
txtTd.valign="top";
txtTd.className="msg_text";
row.appendChild(txtTd);
row = null;
txtTd.innerHTML=message;
DM.clearChildNodes(el);
el.appendChild(topEl);
}
function prepareProcessWindow(id)
{
msg.dialog = $(id);
if (!msg.dialog)
{
msg.dialog = msg.createDialog(id, true);
}
if (msg.busyMark)
{
msg.showDialog();
msg.showMessage("最近一次服务请求尚未执行完毕");
return false;
}
msg.busyMark = true;
return true;
}
function showProgress(message)
{
if (msg.busyMark)
{
msg.showDialog();
msg.showMessage(message);
}
}
function $(id)
{
return document.getElementById(id);
}
function hideDiv(id)
{
var t;
if (id.length) t = $(id);
else t = id;
t.style.display="none";
}
function displayBlockDiv(id)
{
var t;
if (id.length) t = $(id);
else t = id;
t.style.display="block";
}
function finishProcess(id)
{
if (this.busyMark)
{
this.busyMark = false;
}
if (msg != null)
{
if (msg.busyMark)
{
msg.busyMark = false;
}
msg.hideDialog();
}
}
var DM = new Object();
DM.clearChildNodes = function(dom)
{
this.tmpDom = null;
while (dom && dom.lastChild)
{
this.tmpDom = dom.removeChild(dom.lastChild);
if (this.tmpDom.refs)
{//used in IE
for (var i in this.tmpDom.refs)
{
if (this.tmpDom.refs[i].parentNode)
{
var tmpChildDom = this.tmpDom.refs[i].parentNode.removeChild(this.tmpDom.refs[i]);
delete this.tmpChildDom;
delete tmpDom.refs[i];
}
}
}
delete this.tmpDom;
}
}