Фрейм главного окна чата.
| Код | <script type="text/javascript" src='jscripts/js_chat.js'></script> <script type="text/javascript" src='jscripts/ips_xmlhttprequest.js'></script> <script type="text/javascript"> <!-- var GetChaturl = "index.php?act=chat&CODE=load"; var SendChaturl = "index.php?act=chat&CODE=send"; var lastID = $lastID; window.onload = init_chat; --> </script>
|
ips_xmlhttprequest.js':
| Код | //------------------------------------------------------------------------------ // IPS XML HTTP REQUEST //------------------------------------------------------------------------------ // Supports Safari, Mozilla 1.3+ (Firefox, etc) and IE 5.5+ // (c) 2005 Invision Power Services, Inc. // http://www.invisionpower.com //------------------------------------------------------------------------------
try { if ( ! use_charset ) { var use_charset = ''; } } catch( e ) { }
/*--------------------------------------------*/ // New object /*--------------------------------------------*/
function ajax_request() { this.isIE = false; this.allow_use = use_enhanced_js ? true : false; this.xmlhandler = null; this.error_string = ''; this.nocache = true; this.do_request_functon = function() {} this.loading_fired = 0; this.centerdiv = null; }
/*--------------------------------------------*/ // Initiate /*--------------------------------------------*/
ajax_request.prototype.xml_init = function() { try { //------------------------------------------------ // Moz, Safari, Opera //------------------------------------------------
this.xmlhandler = new XMLHttpRequest(); this.ie = false; this.allow_use = true; return true; } catch(e) { try { //------------------------------------------------ // IE //------------------------------------------------
this.xmlhandler = new ActiveXObject('Microsoft.XMLHTTP'); this.ie = true; this.allow_use = true; return true; } catch(e) { this.ie = true; this.allow_use = false; return false; } } }
/*--------------------------------------------*/ // Actually send data /*--------------------------------------------*/
ajax_request.prototype.process = function( url, type, post ) { //------------------------------------------------ // The 'post' variable needs to be in the following format: // // var=content&var2=content&var3=content // // All values need to be escaped with encodeURIComponent(); //------------------------------------------------
type = type == "POST" ? "POST" : "GET";
//------------------------------------------------ // Use nocache where possible... //------------------------------------------------
if ( this.nocache == true && type == 'GET' ) { url = this.nocache_url( url ); }
//------------------------------------------------ // Make sure we're initialized //------------------------------------------------
if ( ! this.xmlhandler ) { this.xml_init(); }
//------------------------------------------------ // Only go when ready //------------------------------------------------
if ( ! this.readystate_not_ready() ) { this.xmlhandler.open(type, url, true);
if ( type == "GET" ) { this.xmlhandler.send(null); } else { if ( typeof( this.xmlhandler.setRequestHeader ) != "undefined" ) { this.xmlhandler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=' + use_charset); }
this.xmlhandler.send( post ); }
if ( this.xmlhandler.readyState == 4 && this.xmlhandler.status == 200 ) { return true; } }
return false; }
/*--------------------------------------------*/ // retrieve text of an XML document element, including // elements using namespaces /*--------------------------------------------*/
ajax_request.prototype.get_element_text_ns = function(prefix, local, parentElem, index) { var result = "";
if ( prefix && this.isIE ) { //------------------------------- // IE //-------------------------------
result = parentElem.getElementsByTagName(prefix + ":" + local)[index]; } else { //------------------------------- // Safari, Gecko //-------------------------------
result = parentElem.getElementsByTagName(local)[index]; }
if ( result ) { if (result.childNodes.length > 1) { return result.childNodes[1].nodeValue; } else { return result.firstChild.nodeValue; } } else { return "n/a"; } }
/*--------------------------------------------*/ // Make sure URL is not cached /*--------------------------------------------*/
ajax_request.prototype.nocache_url = function (url) { var sep = ( -1 < url.indexOf("?") ) ? "&" : "?"; var mydate = new Date(); var newurl = url + sep + "__=" + mydate.getTime(); return newurl; }
/*--------------------------------------------*/ // Takes an array of: // array[ field ] = value // and returns a nice encoded POST string /*--------------------------------------------*/
ajax_request.prototype.format_for_post = function( arrayfields ) { var str = '';
try { for( var i in arrayfields ) { str += i + '=' + this.encodeurl(arrayfields[i]) + '&'; } } catch(e) { }
return str; }
/*--------------------------------------------*/ // Hand roll encode URL to UTF-8 /*--------------------------------------------*/
ajax_request.prototype.encodeurl = function( url ) { //------------------------------- // Ensure we have a string //-------------------------------
url = url.toString();
var regcheck = url.match(/[\x90-\xFF]/g);
if ( regcheck ) { for (var i = 0; i < i.length; i++) { url = url.replace(regcheck[i], '%u00' + (regcheck[i].charCodeAt(0) & 0xFF).toString(16).toUpperCase()); } }
return escape(url).replace(/\+/g, "%2B"); }
/*--------------------------------------------*/ // Check to ensure ready state-ness /*--------------------------------------------*/
ajax_request.prototype.readystate_not_ready = function() { return ( this.xmlhandler.readyState && ( this.xmlhandler.readyState < 4 ) ); }
/*--------------------------------------------*/ // Check to ensure ready state-ness /*--------------------------------------------*/
ajax_request.prototype.readystate_ready_and_ok = function() { return ( this.xmlhandler.readyState == 4 && this.xmlhandler.status == 200 ) ? true : false; }
/*--------------------------------------------*/ // Onready state change event handler /*--------------------------------------------*/
ajax_request.prototype.onreadystatechange = function( event ) { //------------------------------------------------ // Make sure we're initialized //------------------------------------------------
if ( ! this.xmlhandler ) { this.xml_init(); }
//------------------------------------------------ // Make sure its a function event //------------------------------------------------
if ( typeof(event) == 'function' ) { this.xmlhandler.onreadystatechange = event; } }
/*--------------------------------------------*/ // Show loading layer /*--------------------------------------------*/
ajax_request.prototype.show_loading = function( message ) { if ( ! this.loading_fired ) { this.loading_fired = 1;
//------------------------------------------------ // Change text? //------------------------------------------------
if ( message ) { document.getElementById( 'loading-layer-text' ).innerHTML = message; }
this.centerdiv = new center_div(); this.centerdiv.divname = 'loading-layer'; this.centerdiv.move_div(); }
return; }
/*--------------------------------------------*/ // Hide loading layer /*--------------------------------------------*/
ajax_request.prototype.hide_loading = function() { try { if ( this.centerdiv && this.centerdiv.divobj ) { this.centerdiv.hide_div(); } } catch(e) { }
this.loading_fired = 0;
return; }
/*--------------------------------------------*/ // IPB thinks we can use fancy JS, lets see... /*--------------------------------------------*/
if ( use_enhanced_js ) { use_enhanced_js = ajax_request.prototype.xml_init() ? 1 : 0; }
|
js_chat.js:
| Код | //------------------------------------------------------------------------------ // XML HTTP REQUEST: CHAT //------------------------------------------------------------------------------ // Supports Safari, Mozilla 1.3+ (Firefox, etc) and IE 5.5+ // © 2006 | Elfet // http://elfet.mellon.ru // http://www.mellon.ru // Elf Chat //------------------------------------------------------------------------------
/*--------------------------------------------*/ // INIT VARS /*--------------------------------------------*/
var use_enhanced_js = 1; var xmlobj_ch; var xmlobj;
function init_chat() { xmlobj_ch = new ajax_request(); xmlobj_ch.onreadystatechange( do_request_function ); xmlobj = new ajax_request(); xmlobj.onreadystatechange( do_done_function ); load(); }
/*--------------------------------------------*/ // Main function to do on request // Must be defined first!! /*--------------------------------------------*/
do_request_function = function() { //---------------------------------- // Ignore unless we're ready to go //----------------------------------
if ( ! xmlobj_ch.readystate_ready_and_ok() ) { // Could do a little loading graphic here? return; }
//---------------------------------- // INIT //----------------------------------
var js = xmlobj_ch.xmlhandler.responseText;
eval(js);
}
function load() { //---------------------------------- // LOAD JS //----------------------------------
xmlobj_ch.process( GetChaturl + '&lastID=' + lastID );
setTimeout('load();',4000);
}
/*--------------------------------------------*/ // Main function to send // Must be defined first!! /*--------------------------------------------*/
do_done_function = function() { //---------------------------------- // Ignore unless we're ready to go //----------------------------------
if ( ! xmlobj.readystate_ready_and_ok() ) { // Could do a little loading graphic here? return; }
oSubmit = frames['bar'].document.forms['chatForm'].elements['submit']; oSubmit.disabled = false; load();
}
function sendComment() {
currentChatText = encodeURIComponent( frames['bar'].document.forms['chatForm'].elements['chatbarText'].value );
if (currentChatText != '') {
param = 'text=' + currentChatText;
xmlobj.process( SendChaturl , "POST" , param );
frames['bar'].document.forms['chatForm'].elements['chatbarText'].value = '';
frames['bar'].document.forms['chatForm'].elements['chatbarText'].focus();
oSubmit = frames['bar'].document.forms['chatForm'].elements['submit'];
oSubmit.disabled = true; } }
function inp(date,type,name,uname,id,msg) { frames['tv'].goinput(date,type,name,uname,id,msg); }
function onc() { frames['online'].location='index.php?act=chat&CODE=online' + '&rand=' +Math.floor(Math.random() * 1000000); }
function qm(id) { window.open('index.php?act=hello&mid=' + id,'hello_' + id,'width=600,height=300,resizable=yes,scrollbars=auto'); }
|
Не могу понять почему не работает моя функция load() Первый раз при загрузке чата, она срабатывает, а потом нет Сообщения вроде посылаються, но не срабатывает функция load() из do_done_function() и не срабатывает oSubmit = frames['bar'].document.forms['chatForm'].elements['submit']; oSubmit.disabled = false; Что соовсем странно, так как это должно выполняться.
Прошу помоч мне разобраться! Буду очень благодарен?
PS И как здесь ещё использовать xml с xmlhttpreguest? Вместо моего возвращающего js в eval? PPS И как лучшее вообще организовать структуру чата? У меня она на фреймал, но это вроде не по стандартам xhtml? |