louisnan 发表于 2013-1-29 07:54:38

AJAX留言本--简易版

function clearForm(){ //清空表单的函数   getObject("username").value="";   getObject("qq").value="";   getObject("email").value="";   getObject("content").value="";    }       function addNew(){ //更新留言,实质上是重新去服务器提取第一页的留言   if(/LastDate=([^;]+)/.test(document.cookie)){ //删除cookie中记录的时间,模拟第一次浏览,防止重复更新留言      var exp=new Date();      exp.setTime(exp.getTime()-1);      document.cookie="LastDate="+RegExp.$1+";expires="+exp.toGMTString();   }   getList(1);//更新cookie后更新第一页的留言    }       function makBookStr(username,sex,qq,email,content,pubdate){ //创建一条留言的文本串   if((email=unescape(email))!="不告诉你"){      email='<a href="mailto:'+email+'" title="'+email+'">给我写信</a>';   }   var tempStr='<div >\       <div ><div >'+sex+'</div><strong>昵称:</strong>'+unescape(username)+'</div>\       <div ><strong>QQ:</strong>'+unescape(qq)+' <strong>E-mail:</strong>'+email+'<strong>发表时间:</strong>'+pubdate+'</div>\       <div >\      <strong>留言内容:</strong>\      <p>'+unescape(content)+'</p>\       </div>\      </div>';   return tempStr;    }       function writeBookArr(Str){ //输出服务器返回的留言内容   if(Str!=0){      var bookList=eval("new Array("+Str+")");      var allStr="";      for(var i=0;i<bookList.length;i++){       allStr+=makBookStr(bookList.username,bookList.sex,bookList.qq,bookList.email,bookList.content,bookList.pubdate);       }   }else{ //返回0说明没有留言      allStr="<div tips\">暂时还没有留言!</div>"    }   getObject("bookList").innerHTML=allStr;    }       function getList(page){ //获取指定页的留言   getObject("bookList").innerHTML="<div tips\">留言加载中....请稍后!</div>"; //清除原来显示的内容   var xhr=XHR();   xhr.open("GET", "getRecord.asp?page="+page+"&r="+Math.random(), true);   xhr.onreadystatechange=function(){      if(xhr.readyState==4){       if(xhr.status==200){      writeBookArr(xhr.responseText);      getPage(); //更新分页信息       }else{      alert("获取留言失败!请刷新重试!");      }      }         }   xhr.send(null);    }       function upDateList(){//如果有新留言,则更新留言   var xhr=XHR();   xhr.open("GET", "getRecord.asp?act=getUpdate&r="+Math.random(), true);   xhr.onreadystatechange=function(){      if(xhr.readyState==4){       if(xhr.status==200){      //如果存在LastDate(即不是第一次访问)并且时间与服务器最新留言时间不同则更新留言      if(/LastDate=([^;]+)/.test(document.cookie) && unescape(RegExp.$1)!=xhr.responseText){         getList();//如果cookie和服务器返回的最新留言时间不同则更新留言      }      document.cookie="LastDate="+escape(xhr.responseText); //更新cookie中最新留言的时间       }      }         }   xhr.send(null);   setTimeout("upDateList()",15000); //每15秒更新一次留言    }       function getPage(){ //更新分页信息   getObject("pageList").innerHTML="<div loadPage\">分页信息加载中....请稍后!</div>"; //清除原来显示的内容   var xhr=XHR();   xhr.open("GET", "getRecord.asp?act=getPageInfo&r="+Math.random(), true);   xhr.onreadystatechange=function(){      if(xhr.readyState==4){       if(xhr.status==200){      //Result=记录数|每页记录数|页数|当前页      var Result=xhr.responseText.split("|");      var tempPageStr=new Array("<div>共有<span num\">"+Result+"</span>条留言 每页<span num\">"+Result+"</span>条留言 当前第<span num\">"+Result+"/"+Result+"</span>页 转向第<select page\" getList(this.value)\" style=\"width:50px;\">");      for(var i=1;i<=Result;i++){         if(i!=Result)          tempPageStr="<option value=\""+i+"\">"+i+"</option>";         else         tempPageStr="<option selected=\"selected\" value=\""+i+"\">"+i+"</option>";      }      tempPageStr="</select>页</div>";      var ResultStr=tempPageStr.join('');      getObject("pageList").innerHTML=ResultStr;       }else{      alert("获取分页信息失败!请刷新重试!");      }      }         }   xhr.send(null);    }       function changeStyle(id){ //切换样式   var stylesheet=getObject("color").href="color/color"+id+".css";   document.cookie="stylesheet="+escape(stylesheet); //写入Cookie    }       function initStyle(){ //初始化样式,如果cookie存在样式,则加载cookie样式,否则加载默认样式      if(/stylesheet=([^;]+)/.test(document.cookie))       getObject("color").href=unescape(RegExp.$1);    }    window.onload=function(){initStyle();getList();upDateList()} //网页加载完成后加载初始化样式
页: [1]
查看完整版本: AJAX留言本--简易版