⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajaxrecordset.js

📁 《深入浅出Ajax》 源代码
💻 JS
字号:
//定义AjaxRecordSet类,由该类来进行数据访问以及封返回结果
function AjaxRecordSet() {
	//属性
    this.url=null;			//访问服务器的url
	this.method="POST";		//访问服务器的方式
	this.async=false;		//是否以异步方式访问数据库
	this.ars=null;			//AjaxRecordSet数据存贮对象
	this.arsCount=0;		//当前AjaxRecordSet对象中的记录数
	this.fieldCount=0;      //字段数
	this.arsCurRec=0;		//当前AjaxRecordSet中的当前记录号
	this.xmlHttp=null;		//XMLHttpRequest对象
	this.xmlDoc=null;		//XMLDOC对象
	this.isOpen=false;		//当前的AjaxRecordSet对象是否已经打开
	//方法
	this.callBack = null;
	this.setCallBack=setArsCallBack;
	this.open=openArs;
	this.close=closeArs;
	this.next=nextArs;
	this.prev=prevArs;
	this.first=firstArs;
	this.last=lastArs;
	this.getCount=getCountArs;
	this.getFieldCount=getFieldCountArs;
	this.getFieldAt=getFieldAtArs;
	this.setUrl=setArsUrl;
	this.setMethod=setArsMethod;
    this.getUrl=getArsUrl;
	this.getMethod=getArsMethod;
}
//打开一个AjaxRecordSet结果集
function openArs() {
	if(this.xmlDoc==null) {
		this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(this.callBack!=null) {
		this.async=true;
		var handle = this;
		this.xmlHttp.onreadystatechange = function () {
			//当XMLHttpRequest的readyState状态为4时,表示已经从服务器得到响应
			if (handle.xmlHttp.readyState==4) {
			   //判断从服务器得到的响应状态,200表示正确响应
				if (handle.xmlHttp.status == 0||handle.xmlHttp.status == 200) {
					//得到响应的文本内容 
					//防止没加载完就读取DOM,从而报对象找不到
					handle.xmlDoc.async =false;
					handle.xmlDoc.loadXML(handle.xmlHttp.responseText);
					handle.ars = handle.xmlDoc.documentElement;
					handle.arsCount = handle.ars.childNodes.length;
					handle.fieldCount = handle.ars.childNodes.item(0).childNodes.length;
					handle.arsCurRec = 0;
					handle.isOpen=true;
					//数据处理完毕,回调用户设置的回调函数
					handle.callBack();
				}
			}
		}
	} else this.async=false;
    this.xmlHttp.Open(this.method,this.url,this.anync);
	//this.xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
	//this.xmlHttp.overrideMimeType("text/html");
    this.xmlHttp.send(null);
	if(!this.async) {
		//非异步情况下,直接可以处理结果
		//防止没加载完就读取DOM,从而报对象找不到
		this.xmlDoc.async =false;
		this.xmlDoc.loadXML(this.xmlHttp.responseText);
		this.ars = this.xmlDoc.documentElement;
		this.arsCount = this.ars.childNodes.length;
		this.fieldCount = this.ars.childNodes.item(0).childNodes.length;
		this.arsCurRec = 0;
		this.isOpen=true;
	}
}
//关闭一个AjaxRecordSet结果集
function closeArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return;
	}
	this.arsCurRec=0;
    this.arsCount=0;
	this.ars=null;
	this.callBack=null;
	this.xmlDoc=null;
	this.cmlHttp=null;
	this.isOpen=false;
}
//设置异步情况下的回调函数
function setArsCallBack(handle){
	this.callBack=handle;
}
//获取AjaxRecordSet结果集中的记录数
function getCountArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return null;
	}
	return this.arsCount;
}
//获取AjaxRecordSet结果集中的字段数
function getFieldCountArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return null;
	}
	return this.fieldCount;
}
//获取AjaxRecordSet结果集中当前记录第index例的值
function getFieldAtArs(index){
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return null;
	}
    var rec = this.ars.childNodes.item(this.arsCurRec);
    if (rec.childNodes.length < index + 1) return rec.childNodes.item(0).text;
    else return rec.childNodes.item(index).text;
}
//在AjaxRecordSet结果集中移向下一条记录,如果到了最尾部,则返回false
function nextArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return false;
	}
	if(this.arsCurRec<(this.arsCount-1)) {
		this.arsCurRec++;
		return true;
	} else return false;
}
//在AjaxRecordSet结果集中移向前一条记录,如果到了最首部,则返回false
function prevArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return false;
	}
	if(this.arsCurRec>0) {
		this.arsCurRec--;
		return true;
	} else return false;
}
//在AjaxRecordSet结果集中移向第一条记录
function firstArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return;
	}
	this.arsCurRec=0;
}
//在AjaxRecordSet结果集中移向最后一条记录
function lastArs() {
	if(!this.isOpen) {
		alert("错误:结果集还没有打开!");
		return;
	}
	this.arsCurRec=this.arsCount-1;
}
//设置url
function setArsUrl(url) {
	this.url=url;
}
//获取url
function getArsUrl() {
	return this.url;
}
//设置method
function setArsMethod(method) {
	this.method=method;
}
//获取method
function getArsMethod()  {
	return this.method;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -