php配合ajax实现仿网易文章评论效果

前台代码:

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

JS代码:

<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 "";
}

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());
}

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);
 }
}

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>

转载于:https://my.oschina.net/u/126154/blog/14257

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值