a1exey
Новичок
ajax чат выводит plaintext вместе с хтмл кодом
подскажите пожалуста в чём может быть дело?
вот сам .js файлик, сдаётся мне что в функцию insertNewContent надо что то дописать чтобы ХТМЛ выводился ХТМЛом, только что я не знаю... не силён в яве...подскажите пожалуста.
var GetChaturl = "chat_data_get.php";
var SendChaturl = "chat_data_send.php";
var lastID = -1; //initial value will be replaced by the latest known id
window.onload = initJavaScript;
function initJavaScript() {
// document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off'); //this non standard attribute prevents firefox' autofill function to clash with this script
receiveChatText(); //initiates the first data query
}
//initiates the first data query
function receiveChatText()
{
if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0)
{
httpReceiveChat.open("GET",GetChaturl + '?lastID=' + lastID + '&rand='+Math.floor(Math.random() * 1000000), true);
httpReceiveChat.onreadystatechange = handlehHttpReceiveChat;
httpReceiveChat.send(null);
}
}
//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat()
{
if (httpReceiveChat.readyState == 4)
{
results = httpReceiveChat.responseText.split('---'); //the fields are seperated by ---
if (results.length > 3)
{
for(i=0;i < (results.length-1);i=i+4)
{ //goes through the result one message at a time
insertNewContent(results[i+1],results[i+2],results[i+3]); //inserts the new content into the page
}
lastID = results[results.length-5];
}
setTimeout('receiveChatText();',1000); //executes the next data query in 1 seconds
}
}
//inserts the new content into the page
function insertNewContent(liTime,liName,liText)
{
insertO = document.getElementById("outputList");
oLi = document.createElement('div');
oTime = document.createTextNode(liTime);
oName = document.createTextNode(liName+': ');
oText = document.createTextNode(liText);
oLi.appendChild(oTime);
oLi.appendChild(oName);
oLi.appendChild(oText);
insertO.insertBefore(oLi, insertO.firstChild);
}
//stores a new comment on the server
function sendComment()
{
currentChatText = document.forms['chatForm'].elements['chatbarText'].value;
if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0))
{
param = 'c='+ currentChatText;
httpSendChat.open("POST", SendChaturl, true);
httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
httpSendChat.onreadystatechange = handlehHttpSendChat;
httpSendChat.send(param);
document.forms['chatForm'].elements['chatbarText'].value = '';
}
else
{
setTimeout('sendComment();',1000);
}
}
//deals with the servers' reply to sending a comment
function handlehHttpSendChat()
{
if (httpSendChat.readyState == 4)
{
receiveChatText(); //refreshes the chat after a new comment has been added (this makes it more responsive)
}
}
//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpSendChat = getHTTPObject();
в итоге на выводе что то типа "12:40:02 Вика : <img border=0 src="http://tracker.starlink.ru/pic/smilies/question.gif" alt=":?:"> <b>текст.</b> "
подскажите пожалуста в чём может быть дело?
вот сам .js файлик, сдаётся мне что в функцию insertNewContent надо что то дописать чтобы ХТМЛ выводился ХТМЛом, только что я не знаю... не силён в яве...подскажите пожалуста.
var GetChaturl = "chat_data_get.php";
var SendChaturl = "chat_data_send.php";
var lastID = -1; //initial value will be replaced by the latest known id
window.onload = initJavaScript;
function initJavaScript() {
// document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off'); //this non standard attribute prevents firefox' autofill function to clash with this script
receiveChatText(); //initiates the first data query
}
//initiates the first data query
function receiveChatText()
{
if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0)
{
httpReceiveChat.open("GET",GetChaturl + '?lastID=' + lastID + '&rand='+Math.floor(Math.random() * 1000000), true);
httpReceiveChat.onreadystatechange = handlehHttpReceiveChat;
httpReceiveChat.send(null);
}
}
//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat()
{
if (httpReceiveChat.readyState == 4)
{
results = httpReceiveChat.responseText.split('---'); //the fields are seperated by ---
if (results.length > 3)
{
for(i=0;i < (results.length-1);i=i+4)
{ //goes through the result one message at a time
insertNewContent(results[i+1],results[i+2],results[i+3]); //inserts the new content into the page
}
lastID = results[results.length-5];
}
setTimeout('receiveChatText();',1000); //executes the next data query in 1 seconds
}
}
//inserts the new content into the page
function insertNewContent(liTime,liName,liText)
{
insertO = document.getElementById("outputList");
oLi = document.createElement('div');
oTime = document.createTextNode(liTime);
oName = document.createTextNode(liName+': ');
oText = document.createTextNode(liText);
oLi.appendChild(oTime);
oLi.appendChild(oName);
oLi.appendChild(oText);
insertO.insertBefore(oLi, insertO.firstChild);
}
//stores a new comment on the server
function sendComment()
{
currentChatText = document.forms['chatForm'].elements['chatbarText'].value;
if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0))
{
param = 'c='+ currentChatText;
httpSendChat.open("POST", SendChaturl, true);
httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
httpSendChat.onreadystatechange = handlehHttpSendChat;
httpSendChat.send(param);
document.forms['chatForm'].elements['chatbarText'].value = '';
}
else
{
setTimeout('sendComment();',1000);
}
}
//deals with the servers' reply to sending a comment
function handlehHttpSendChat()
{
if (httpSendChat.readyState == 4)
{
receiveChatText(); //refreshes the chat after a new comment has been added (this makes it more responsive)
}
}
//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpSendChat = getHTTPObject();
в итоге на выводе что то типа "12:40:02 Вика : <img border=0 src="http://tracker.starlink.ru/pic/smilies/question.gif" alt=":?:"> <b>текст.</b> "