Asp.net源码专业站
首页->聊天计数->Ajax实现的在线聊天室>>js/AjaxFunctions.js>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Ajax实现的在线聊天室
当前文件:文件类型 AjaxChat/js/AjaxFunctions.js[3K,2009-6-12 11:31:35]打开代码结构图
普通视图
		            
1//Global variables 2var timeID; 3var refreshRate = 2000; // two seconds 4var rnd = Math.random(); 5 6var isFirefox; 7var isIE; 8 9//var XmlHttp; 10var AjaxServerPageName; 11AjaxServerPageName = "Server.aspx"; 12 13//Creating and setting the instance of appropriate XMLHTTP Request object to a 揦mlHttp?variable 14function getAjax() 15{ 16 var XmlHttp; 17 18 //Creating object of XMLHTTP in IE 19 try 20 { 21 XmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 22 } 23 catch(e) 24 { 25 try 26 { 27 XmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 28 } 29 catch(oc) 30 { 31 XmlHttp = null; 32 } 33 } 34 //Creating object of XMLHTTP in Mozilla and Safari 35 if(!XmlHttp && typeof XMLHttpRequest != "undefined") 36 { 37 XmlHttp = new XMLHttpRequest(); 38 } 39 return XmlHttp; 40} 41 42// Get browser type 43function sniffBrowserType() { 44 isFirefox = ( navigator.appName == "Netscape" ); 45 isIE = (navigator.appName == "Microsoft Internet Explorer" ); 46} 47 48 49// Capture the enter key on the input box and post message 50function captureReturn( event ) 51{ 52 if(event.which || event.keyCode) 53 { 54 if ((event.which == 13) || (event.keyCode == 13)) 55 { 56 postText(); 57 return false; 58 } 59 else { 60 return true; 61 } 62 } 63} 64 65// Start the update timer 66function setTimers() 67{ 68 timeID = window.setTimeout( "updateAll()", refreshRate ); 69} 70 71// Start to update and reset the update timer 72function updateAll() 73{ 74 window.clearTimeout( timeID ); 75 getUserList(); 76 setTimers(); 77} 78 79function getUserList() 80{ 81 rnd++; 82 url = 'Server.aspx?action=UserList&session=' + rnd; 83 req = getAjax(); 84 85 req.onreadystatechange = function(){ 86 87 if( req.readyState == 4 && req.status == 200 ) { 88 89 obj = getElement( "userlist" ); 90 obj.innerHTML = req.responseText; 91 getBufferText(); 92 } 93 94 } 95 96 req.open( 'GET', url, true ); 97 req.send( null ); 98} 99 100function getBufferText() 101{ 102 rnd++; 103 url = 'Server.aspx?action=GetMsg&session=' + rnd; 104 req = getAjax(); 105 106 req.onreadystatechange = function(){ 107 108 if( req.readyState == 4 && req.status == 200 ) { 109 110 obj = getElement( "chatbuffer" ); 111 obj.innerHTML = req.responseText; 112 scrollChatPane(); 113 //FocusWindow(); 114 } 115 } 116 117 req.open( 'GET', url , true ); 118 req.send( null ); 119} 120 121function postText() 122{ 123 rnd++; 124 chatbox = getElement( "mytext" ); 125 chat = chatbox.value; 126 chatbox.value = ""; 127 128 userid = location.search.substring( 1, location.search.length ); 129 url = 'Server.aspx?action=PostMsg&u=' + userid + '&t=' + encodeURIComponent(chat) + '&session=' + rnd; 130 131 req = getAjax(); 132 133 req.onreadystatechange = function(){ 134 135 if( req.readyState == 4 && req.status == 200 ) { 136 updateAll(); 137 } 138 139 } 140 141 req.open( 'GET', url, true ); 142 req.send( null ); 143} 144 145function getElement( id ) 146{ 147 if( isIE ) { 148 return document.all[ id ]; 149 } 150 else { 151 return document.getElementById( id ); 152 } 153} 154 155function showLoadScreen() 156{ 157 var loading = "<div style=\"text-align:center;color:red;\"><h5>Loading...</h5></div>"; 158 159 chat = getElement( "chatbuffer" ); 160 chat.innerHTML = loading; 161 162 user = getElement( "userlist" ); 163 user.innerHTML = loading; 164} 165 166function scrollChatPane() 167{ 168 var obj = document.getElementById("chatpane"); 169 obj.scrollTop = obj.scrollHeight; 170} 171 172function setFocus(ControlName) 173{ 174 var control = document.getElementById(ControlName); 175 if( control != null ) 176 { 177 control.focus(); 178 } 179} 180function FocusWindow() 181{ 182 183 //TODO: FireFox doesn't work? 184 window.focus(); 185 186}
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:Ajax实现的在线聊天室
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146