网站地图    收藏   

主页 > php专栏 > php综合实列 >

ajax php 聊天室实例代码(5) - 综合实例

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] functionaddonline(name){if($(name))return;vard1=document createelement(div);d1 id=name;d1 innerhtml=name;d1 classname=online;$...

ajax php 聊天室实例代码(5)

  1. function addonline(name) 
  2. { 
  3.  if ($(name)) return; 
  4.  var d1 = document.createelement("div"); 
  5.  d1.id = name; 
  6.  d1.innerhtml = name; 
  7.  d1.classname = "online"; 
  8.  $('div_online').appendchild(d1); 
  9. } 
  10. touchme(); 
  11. function check_send(e) 
  12. { 
  13.  if (!e) e = window.event; 
  14.  var obj = $('chat_word'); 
  15.  if (isie) obj.style.height = obj.scrollheight+3; 
  16.  if (e.keycode == 13) 
  17.  { 
  18.   if ((!e.shiftkey && !e.altkey && !e.ctrlkey) || !isie) 
  19.   { 
  20.    chat_send(); 
  21.    obj.style.height = 20; 
  22.    return false; 
  23.   } 
  24.   else if (isie) obj.style.height = obj.scrollheight+18; 
  25.  } 
  26.  return true; 
  27. } 
  28. var send_ajax; 
  29.  
  30. send_ajax_change  = function() 
  31. { 
  32.  if (send_ajax.readystate == 4) 
  33.  { 
  34.   if (send_ajax.status != 200) 
  35.   { 
  36.    send_ajax_error(); 
  37.    return; 
  38.   } 
  39.   if (debug) alert("send_ajax response:"+send_ajax.responsetext); 
  40.   if (send_ajax.responsetext.indexof("name")!=-1) 
  41.   { 
  42.    alert('已经有人使用你的昵称了'); 
  43.    $('chat_user').value = ""; 
  44.    $('chat_user').focus(); 
  45.   } 
  46.   else if (send_ajax.responsetext.indexof("repeat")!=-1) 
  47.   { 
  48.    $('chat_word').value = window.lastcontent; 
  49.   } 
  50.    
  51.   on_send_ok(); 
  52.    
  53.   if (!window.loading) 
  54.   { 
  55.    window.dotouch = true; 
  56.    load_word(); 
  57.   } 
  58.   $('chat_word').disabled = false; 
  59.   $('chat_word').focus(); 
  60.  } 
  61. } 
  62. function on_send_begin() 
  63. { 
  64.  with($('chat_word')) 
  65.  { 
  66.   disabled = true; 
  67.   style.backgroundcolor = "#eeeeee"; 
  68.  } 
  69.  window.sending = 1; 
  70. } 
  71. function on_send_ok() 
  72. { 
  73.  window.sending = 0; 
  74.  with($('chat_word')) 
  75.  { 
  76.   value = ''; 
  77.   disabled = false; 
  78.   focus(); 
  79.   style.backgroundcolor = "#ffffff"; 
  80.  }  
  81. } 
  82. function on_send_error() 
  83. { 
  84.  window.sending = 0; 
  85.  with($('chat_word')) 
  86.  { 
  87.   disabled = false; 
  88.   focus(); 
  89.   style.backgroundcolor = "#ffffff"; 
  90.  } 
  91. } 
  92. function send_ajax_error() 
  93. { 
  94.  alert('error 103nwhen send wordsnnyou can send them again!'); 
  95.  $('chat_word').value = window.lastcontent; 
  96.  window.sending = 0; 
  97.  on_send_error(); 
  98. } 
  99. function chat_send() 
  100. { 
  101.  send_ajax = createajax(); 
  102.  send_ajax.open('post','<?php echo basename(__file__);?>',true); 
  103.  send_ajax.setrequestheader("content-type", "application/x-www-form-urlencoded"); 
  104.  send_ajax.onreadystatechange = send_ajax_change; 
  105.  var urlstring = ''; 
  106.  var name = $('chat_user').value.replace("n",""); 
  107.  var content = $('chat_word').value;  
  108.  var bold = ($('input_bold').checked)?"bold":""; 
  109.  var size = parseint($('input_size').value); 
  110.  var font = $('input_font').value; 
  111.   
  112.  if (name == "") 
  113.  { 
  114.   alert('please enter your nick name first!!'); 
  115.   $('chat_user').focus(); 
  116.   return; 
  117.  } 
  118.   
  119.  if (content == "" || content == "n" || content == "nn" || content == "nnn") 
  120.  { 
  121.   alert('please enter your words!'); 
  122.   $('chat_word').focus(); 
  123.   $('chat_word').value = ""; 
  124.   return; 
  125.  } 
  126.  if (size>100) size = 100; 
  127.  else if (size<0) size = 1; 
  128.   
  129.  urlstring+= "action=write"; 
  130.  urlstring+= "&name="+encode(name); 
  131.  urlstring+= "&content="+encode(content); 
  132.  urlstring+= "&bold="+bold; 
  133.  urlstring+= "&color="+window.color; 
  134.  urlstring+= "&size="+size; 
  135.  urlstring+= "&font="+font; 
  136.  urlstring+= "&room="+room; 
  137.  window.sending = 1; 
  138.  window.lastcontent = content; 
  139.  on_send_begin(); 
  140.  if (debug) alert("sending:"+urlstring); 
  141.   
  142.  send_ajax.send(urlstring); 
  143.  settimeout("if (window.sending) send_ajax.abort(); on_send_error();",5000); 
  144.  setcookie("chatusername",$('chat_user').value); 
  145. } 
  146. function resize(s) 
  147. { 
  148.  var o = $('div_contents').style; 
  149.  var h = parseint(o.height); 
  150.  h = (s)?h+50:h-50; 
  151.  if (h<=50 || h>=3000) return; 
  152.  o.height = h; 
  153.  $('div_contents').scrolltop = 655350; 
  154. } 
  155. function clearall() 
  156. { 
  157.  $('div_contents').innerhtml = ""; 
  158. } 
  159. </script> 
  160. </center> 
  161. </body> 
  162. </html> 
  163. <?php 
  164. } 
  165. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论