Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
function http_object() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.'; } } var fieldname = 'chat'; var last_time = 0; var xmlHttp = http_object(); var last_id = {LAST_ID}; var type = 'receive'; var post_time = {TIME}; var read_interval = 1500; var interval = setInterval('handle_send("read", last_id);', read_interval); var objDiv = document.getElementById(fieldname); var chatscroll = new Object(); chatscroll.Pane = function(scrollContainerId) { this.bottomThreshold = 25; this.scrollContainerId = scrollContainerId; } chatscroll.Pane.prototype.activeScroll = function() { var scrollDiv = document.getElementById(this.scrollContainerId); var currentHeight = 0; if (scrollDiv.scrollHeight > 0) currentHeight = scrollDiv.scrollHeight; else if (objDiv.offsetHeight > 0) currentHeight = scrollDiv.offsetHeight; if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold) scrollDiv.scrollTop = currentHeight; scrollDiv = null; } var divScroll = new chatscroll.Pane('chatbox'); function handle_send(mode, f) { if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { indicator_switch('on'); type = 'receive'; param = 'mode=' + mode; param += '&last_id=' + last_id; param += '&last_time=' + last_time; param += '&last_post=' + post_time; param += '&read_interval=' + read_interval; if (mode == 'add' && document.text.message.value != '') { type = 'send'; for(var i = 0; i < f.elements.length; i++) { elem = f.elements[i]; param += '&' + elem.name + '=' + encodeURIComponent(elem.value); } document.text.message.value = ''; } else if (mode == 'delete') { type = 'delete'; param += '&chat_id=' + f; } xmlHttp.open("POST", '{FILENAME}', true); xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlHttp.onreadystatechange = handle_return; xmlHttp.send(param); } } function handle_return() { if (xmlHttp.readyState == 4) { if (type != 'delete') { results = xmlHttp.responseText.split('---'); if (results[1]) { if (last_id == 0) { document.getElementById(fieldname).innerHTML = results[0]; divScroll.activeScroll(); } else { document.getElementById(fieldname).innerHTML = document.getElementById(fieldname).innerHTML + results[0]; divScroll.activeScroll(); } last_id = results[1]; if (results[2]) { document.getElementById('whois_online').innerHTML = results[2]; last_time = results[3]; if (results[4] != read_interval * 1000) { window.clearInterval(interval); read_interval = results[4] * 1000; interval = setInterval('handle_send("read", last_id);', read_interval); //document.getElementById('update_seconds').innerHTML = results[4]; } post_time = results[5]; } } } indicator_switch('off'); } } function delete_post(chatid) { document.getElementById('p' + chatid).style.display = 'none'; handle_send('delete', chatid); } function indicator_switch(mode) { if(document.getElementById("act_indicator")) { var img = document.getElementById("act_indicator"); if(img.style.visibility == "hidden" && mode == 'on') { img.style.visibility = "visible"; } else if (mode == 'off') { img.style.visibility = "hidden" } } }
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
<!-- IF S_GET_CHAT --> <!-- IF S_READ or S_ADD --> <!-- BEGIN chatrow --> <div id="p{chatrow.MESSAGE_ID}" class="post bg{chatrow.CLASS}"> <!-- IF U_ACP or U_MCP --> <a href="javascript:void({chatrow.MESSAGE_ID})" title="{L_DELETE_POST}" onclick="delete_post('{chatrow.MESSAGE_ID}')" style="float:right;">{L_DELETE_POST}</a> <!-- ENDIF --> <span class="author" id="profile{postrow.POST_ID}">{chatrow.USERNAME_FULL}</span> <!-- <span class="time"> ({chatrow.TIME}):</span> --> <span class="content">{chatrow.MESSAGE}</span> </div> <!-- END chatrow -->---{LAST_ID} <!-- IF S_WHOISONLINE -->--- <!-- BEGIN whoisrow --> <div> <div class="inner"> <div class="user"><img src="{T_IMAGESET_PATH}/{whoisrow.USER_STATUS}.png" class="online_img" /> : {whoisrow.USERNAME_FULL}</div> </div> </div> <!-- END whoisrow -->---{LAST_TIME}---{DELAY}---{LAST_POST} <!-- ENDIF --> <!-- ENDIF --> <!-- ELSE --> <!-- IF S_CHAT --> <!-- INCLUDE overall_header.html --> <!-- ENDIF --> <script type="text/javascript"> <!-- function http_object() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.'; } } var fieldname = 'chat'; var last_time = 0; var xmlHttp = http_object(); var last_id = {LAST_ID}; var type = 'receive'; var post_time = {TIME}; var read_interval = 1500; var interval = setInterval('handle_send("read", last_id);', read_interval); var objDiv = document.getElementById(fieldname); var chatscroll = new Object(); chatscroll.Pane = function(scrollContainerId) { this.bottomThreshold = 25; this.scrollContainerId = scrollContainerId; } chatscroll.Pane.prototype.activeScroll = function() { var scrollDiv = document.getElementById(this.scrollContainerId); var currentHeight = 0; if (scrollDiv.scrollHeight > 0) currentHeight = scrollDiv.scrollHeight; else if (objDiv.offsetHeight > 0) currentHeight = scrollDiv.offsetHeight; if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold) scrollDiv.scrollTop = currentHeight; scrollDiv = null; } var divScroll = new chatscroll.Pane('chatbox'); function handle_send(mode, f) { if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { indicator_switch('on'); type = 'receive'; param = 'mode=' + mode; param += '&last_id=' + last_id; param += '&last_time=' + last_time; param += '&last_post=' + post_time; param += '&read_interval=' + read_interval; if (mode == 'add' && document.text.message.value != '') { type = 'send'; for(var i = 0; i < f.elements.length; i++) { elem = f.elements[i]; param += '&' + elem.name + '=' + encodeURIComponent(elem.value); } document.text.message.value = ''; } else if (mode == 'delete') { type = 'delete'; param += '&chat_id=' + f; } xmlHttp.open("POST", '{FILENAME}', true); xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlHttp.onreadystatechange = handle_return; xmlHttp.send(param); } } function handle_return() { if (xmlHttp.readyState == 4) { if (type != 'delete') { results = xmlHttp.responseText.split('---'); if (results[1]) { if (last_id == 0) { document.getElementById(fieldname).innerHTML = results[0]; divScroll.activeScroll(); } else { document.getElementById(fieldname).innerHTML = document.getElementById(fieldname).innerHTML + results[0]; divScroll.activeScroll(); } last_id = results[1]; if (results[2]) { document.getElementById('whois_online').innerHTML = results[2]; last_time = results[3]; if (results[4] != read_interval * 1000) { window.clearInterval(interval); read_interval = results[4] * 1000; interval = setInterval('handle_send("read", last_id);', read_interval); //document.getElementById('update_seconds').innerHTML = results[4]; } post_time = results[5]; } } } indicator_switch('off'); } } function delete_post(chatid) { document.getElementById('p' + chatid).style.display = 'none'; handle_send('delete', chatid); } function indicator_switch(mode) { if(document.getElementById("act_indicator")) { var img = document.getElementById("act_indicator"); if(img.style.visibility == "hidden" && mode == 'on') { img.style.visibility = "visible"; } else if (mode == 'off') { img.style.visibility = "hidden" } } } --> </script> <style type="text/css"> <!-- #act_indicator { visibility:hidden; } .shouts { width: 85%; height:300px; overflow:auto; float:left; } #chat { width: 100%; text-align:left; } #chat * { margin:0px; padding:0px; min-height:0px; } .post { font-size:1.2em; } .postprofile { min-height: 5px !important; } .chatform { width: 90%; text-align:center; } .onlinelist { width: 15%; overflow:auto; height:300px; } .users { width: 90%; text-align: left; text-indent: 5px; margin-left:auto; margin-right:auto; } .user { width: 95%; font-size: 1.1em; font-family:Verdana, Arial, Helvetica, sans-serif; line-height: 1.4em; } #act_indicator { visibility: hidden; } .chatinput {width: 80% !important;} .online_img { vertical-align:middle; } --> </style> <!-- IF S_CHAT --> <style type="text/css"> <!-- .shouts { height:400px; } .onlinelist { height:400px; } --> </style> <!-- ENDIF --> <div class="forabg" align="left"> <div class="inner"> <span class="corners-top"><span></span></span> <div class="shouts" id="chatbox"> <div id="chat"> <!-- BEGIN chatrow --> <div id="p{chatrow.MESSAGE_ID}" class="post bg{chatrow.CLASS}"> <!-- IF U_ACP or U_MCP --> <a href="javascript:void({chatrow.MESSAGE_ID})" title="{L_DELETE_POST}" onclick="delete_post('{chatrow.MESSAGE_ID}')" style="float:right;">{L_DELETE_POST}</a ><!-- ENDIF --> <span class="author" id="profile{postrow.POST_ID}">{chatrow.USERNAME_FULL}</span> <!-- <span class="time"> ({chatrow.TIME}):</span> --> <span class="content">{chatrow.MESSAGE}</span> </div> <!-- END chatrow --> </div> </div> <div class="onlinelist bg1"> <div class="users" id="whois_online"> <!-- BEGIN whoisrow --> <div> <div class="inner"> <div class="user"><img src="{T_IMAGESET_PATH}/{whoisrow.USER_STATUS}.png" class="online_img" /> {whoisrow.USERNAME_FULL}</div> </div> </div> <!-- END whoisrow --> </div> </div> <span class="corners-bottom"><span></span></span> </div> </div> <!-- IF S_USER_LOGGED_IN --> <div class="forabg"> <div class="inner"> <span class="corners-top"><span></span></span> <div class="chatform" align="center"> <form name="text" id="text" method="post" action="javascript:void(0);" onsubmit="handle_send('add', this)" autocomplete="off"> <strong style="color: white;">{L_MESSAGE}:</strong> <input type="text" tabindex="1" name="message" id="message" class="inputbox chatinput" /> <input type="submit" class="button1" value="{L_SUBMIT}" name="submit" tabindex="6" accesskey="s"/> </form> </div> <span class="corners-bottom"><span></span></span> </div> </div> <!-- ENDIF --> <!-- IF S_CHAT --> <!-- INCLUDE overall_footer.html --> <!-- ENDIF --> <!-- ENDIF -->
Refactorings
No refactoring yet !
This code clumsily handles the client side of an AJAX chat found at http://www.roleplaygateway.com/chat.php - it's slow and unwieldy. The divScroll.activeScroll(); is undependable, too - it doesn't seem to work at times (the div stops scrolling, and the user has to scroll down to see new messages. This is only supposed to occur when they've manually scrolled the chatbox div, and then it should return to the autoscroll behavior when they've scrolled down).