php配合ajax實現仿網易文章評論效果

前臺代碼:javascript

<div class='plding fr'>
<a href='action.php?id="1&action=top_num'>頂一下</a>[5]
</div>php

JS代碼:html

<script language="網頁特效" type="text/javascript">
<!--
// ajax檢測評論是否已經支持過了
function getcookie(c_name){ //檢測cookie
 if (document.cookie.length>0){
  c_start=document.cookie.indexof(c_name + "=")
  if(c_start!=-1){
   c_start=c_start + c_name.length+1;
   c_end=document.cookie.indexof(";",c_start);
   if(c_end==-1) c_end=document.cookie.length;
   return unescape(document.cookie.substring(c_start,c_end));
  }
 }
 return "";
}java

function setcookie(c_name,value,expiredays){ //設置cookie
 var exdate=new date();
 exdate.setdate(exdate.getdate()+expiredays);
 document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.togmtstring());
}ajax

function createxmlhttprequest(){ //建立xmlhttprequest對象
 if(window.activexobject){  //ie
  try {
   return new activexobject("microsoft.xmlhttp");
  } catch(e){
   return;
  }
 }else if(window.xmlhttprequest){ //mozilla,firefox
  try {
   return new xmlhttprequest();
  } catch(e){
   return;
  }
 }
}服務器

function get_top_num(id){ //主調函數
 var get_top_cookie=getcookie('get_top_'+id+'_cookie');
 if(get_top_cookie!=null && get_top_cookie!=""){
  alert('此條評論你已經支持過了');
 }
 else{
  var xmlhttp=createxmlhttprequest();
  var url = "action.php?action=top_num&id="+id;
  if (id==""){
   return false ;
  }
  if (xmlhttp){
   callback = getreadystatehandler(xmlhttp,id);
   xmlhttp.onreadystatechange = callback;
   xmlhttp.open("get", url,true);
   xmlhttp.send(null);
  }
  setcookie('get_top_'+id+'_cookie','istop',1);
 }
}cookie

function getreadystatehandler(xmlhttp,id){  //www.3ppt.com服務器返回後處理函數
 var top_num = document.getelementbyid("top_num_"+id).innerhtml;
 return function (){
  if(xmlhttp.readystate == 4){
   if(xmlhttp.status == 200){
    if (xmlhttp.responsetext==1){
     document.getelementbyid("top_num_"+id).innerhtml=number(top_num)+1;
    }
   }
  }
 }
}函數

//-->
</script>url

相關文章
相關標籤/搜索