科讯sql商业版中用到的ajax空间与分页函数,他们的js代码学习是非常不错的
//ajax控件
functionPageAjax(){
varxhrObj=null;
if(window.XMLHttpRequest)
{
xhrObj=newXMLHttpRequest();
}
elseif(window.ActiveXObject)
{
try{
xhrObj=newActiveXObject("Microsoft.XMLHTTP");
}
catch(e1)
{
try{
xhrObj=newActiveXObject("MSXML2.XMLHTTP");
}
catch(e2){
try{
xhrObj=newActiveXObject("MSXML3.XMLHTTP");
}
catch(e3){
alert("创建Ajax失败:"+e3)
}
}
}
}
else
{
alert("未能识别的浏览器");
}
returnxhrObj;
}
functionPage(curPage,labelid,classid,installdir,url,refreshtype,specialid)
{
this.labelid=labelid;
this.classid=classid;
this.url=url;
if(labelid.substring(0,5)=="{SQL_")
{
varslabelid=labelid.split('(')[0];
slabelid=slabelid.replace("{","");
this.c_obj="c_"+slabelid;
this.p_obj="p_"+slabelid;
}
else
{
this.c_obj="c_"+labelid;
this.p_obj="p_"+labelid;
}
this.installdir=installdir;
this.refreshtype=refreshtype;
this.specialid=specialid;
this.page=curPage;
loadData(1);
}
functionloadData(p)
{this.page=p;
varxhr=newPageAjax();
xhr.open("get",installdir+url+"?labelid="+escape(labelid)+"&classid="+classid+"&refreshtype="+refreshtype+"&specialid="+specialid+"&curpage="+p,true);
xhr.onreadystatechange=function(){
if(xhr.readyState==1)
{
if(p==1)
eval('document.all.'+c_obj).innerHTML="<divalign='center'><imgsrc='"+installdir+"images/loading.gif'>正在连接服务器...</div>";
}
elseif(xhr.readyState==2||xhr.readyState==3)
{
if(p==1)
eval('document.all.'+c_obj).innerHTML="<divalign='center'><imgsrc='"+installdir+"images/loading.gif'>正在读取数据...</div>";
}
elseif(xhr.readyState==4)
{
if(xhr.status==200)
{
varpagearr=xhr.responseText.split("{ks:page}")
varpageparamarr=pagearr[1].split("|");
count=pageparamarr[0];
perpagenum=pageparamarr[1];
pagecount=pageparamarr[2];
itemunit=pageparamarr[3];
itemname=pageparamarr[4];
pagestyle=pageparamarr[5];
getObject(c_obj).innerHTML=pagearr[0];
pagelist();
}
}
}
xhr.send(null);
}
functiongetObject(id)
{
if(document.getElementById)
{
returndocument.getElementById(id);
}
elseif(document.all)
{
returndocument.all[id];
}
elseif(document.layers)
{
returndocument.layers[id];
}
}
functionpagelist()
{
varn=1;
varstatushtml=null;
switch(parseInt(this.pagestyle))
{
case1:
statushtml="共"+this.count+this.itemunit+"<ahref="javascript:homePage(1);"title="首页">首页</a><ahref="javascript:previousPage()"title="上一页">上一页</a><ahref="javascript:nextPage()"title="下一页">下一页</a><ahref="javascript:lastPage();"title="最后一页">尾页</a>页次:<fontcolor=red>"+this.page+"</font>/"+this.pagecount+"页"+this.perpagenum+this.itemunit+this.itemname+"/页";
break;
case2:
statushtml="<ahref='#'>"+this.pagecount+"页/"+this.count+this.itemunit+"</a><ahref="javascript:homePage(1);"title="首页"><fontface=webdings>9</font></a><ahref="javascript:previousPage()"title="上一页"><fontface=webdings>7</font></a>";
varstartpage=1;
if(this.page==10)
startpage=2;
elseif(this.page>10)
startpage=eval((parseInt(this.page/10)-1)*10+parseInt((this.page)%10)+2);
for(vari=startpage;i<=this.pagecount;i++){
if(i==this.page)
statushtml+="<ahref="#"><fontcolor="#ff0000">"+i+"</font></a>"
else
statushtml+="<ahref="javascript:turn("+i+")">"+i+"</a>"
n=n+1;
if(n>10)break;
}
statushtml+="<ahref="javascript:nextPage()"title="下一页"><fontface=webdings>8</font></a><ahref="javascript:lastPage();"title="最后一页"><fontface=webdings>:</font></a>";
statushtml="<spanclass='kspage'>"+statushtml+"</span>";
break;
case4:
statushtml="共"+this.pagecount+"页/"+this.count+this.itemunit+this.itemname+"<ahref="javascript:homePage(1);"title="首页"><fontface=webdings>9</font></a><ahref="javascript:previousPage()"title="上一页"><fontface=webdings>7</font></a>";
for(vari=this.page;i<=this.pagecount;i++){
if(i==this.page)
statushtml+="<b><fontcolor="#ff0000">"+i+"</font></b>"
else
statushtml+="<ahref="javascript:turn("+i+")">["+i+"]</a>"
n=n+1;
if(n>10)break;
}
statushtml+="<ahref="javascript:nextPage()"title="下一页"><fontface=webdings>8</font></a><ahref="javascript:lastPage();"title="最后一页"><fontface=webdings>:</font></a>";
break;
case3:
statushtml="第<fontcolor=#ff000>"+this.page+"</font>页共"+this.pagecount+"页<ahref="javascript:homePage(1);"title="首页"><<</a><ahref="javascript:previousPage()"title="上一页"><</a><ahref="javascript:nextPage()"title="下一页">></a><ahref="javascript:lastPage();"title="最后一页">>></a>"+this.perpagenum+this.itemunit+this.itemname+"/页";
break;
}
statushtml+="第<selectname="goto"onchange="turn(parseInt(this.value));">";
for(vari=1;i<=this.pagecount;i++){
if(i==this.page)
statushtml+="<optionvalue='"+i+"'selected>"+i+"</option>";
else
statushtml+="<optionvalue='"+i+"'>"+i+"</option>";
}
statushtml+="</select>页";
getObject(this.p_obj).innerHTML=statushtml;
}
functionhomePage()
{
if(this.page==1)
alert("已经是首页了!")
else
loadData(1);
}
functionlastPage()
{
if(this.page==this.pagecount)
alert("已经是最后一页了!")
else
loadData(this.pagecount);
}
functionpreviousPage()
{
if(this.page>1)
loadData(this.page-1);
else
alert("已经是第一页了");
}
functionnextPage()
{
if(this.page<this.pagecount)
loadData(this.page+1);
else
alert("已经到最后一页了");
}
functionturn(i)
{
loadData(i);
}