...

View Full Version : js not executing please help?



helloise
08-23-2011, 01:44 PM
i have this line:



echo $new_mail_cnt = '<script type="text/javascript">getMessages();</script>';


then have:


<script language="JavaScript" type="text/javascript">
var receiveReq = getXmlHttpRequestObject();
var mTimer;

function getXmlHttpRequestObject()
{
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.';
}
}

function getMessages()
{
receiveReq = getXmlHttpRequestObject();
if (receiveReq.readyState == 4 || receiveReq.readyState == 0)
{
receiveReq.open("POST", 'getMessage.php', true);
receiveReq.onreadystatechange = handleReceiveMessage();
receiveReq.send(null);
document.getElementById('new_messages').innerHTML = receiveReq.responseXML; //not sure here
}
mTimer = setTimeout('getMessage();',2000);
}

function handleReceiveMessage()
{
if (receiveReq.readyState == 4)
{
var chat_div = document.getElementById('div_chat');
var xmldoc = receiveReq.responseXML;
var message_nodes = xmldoc.getElementsByTagName("message");
}
}
</script>


all i need to do is update/refresh $new_mail_cnt so user can see when new messages come in, but getMessages() does not execute because no alerts pop up??? i dont understand please help?
thank you

devnull69
08-23-2011, 02:08 PM
A few things

1. onreadystatechange expects to be assigned with a function object and not the result/return value of a function call. The difference is the () after the function name!
2. You don't need to have the function call to getMessage() in the getMessage.php. Just call it inside the onreadystatechange callback of your code
3. The first parameter of setTimeout should also be a function object and not a string literal (although both work most of the time)
4. You should only start a new request after the old one finished. So put the window.setTimeout into the onreadystatechange callback for readyState==4


mTimer = window.setTimeout(getMessage, 2000);

5. This line should not be where it is right now


document.getElementById('new_messages').innerHTML = receiveReq.responseXML;

6. Question: Is your response really an XML document or rather HTML?

devnull69
08-23-2011, 02:09 PM
A few things

1. onreadystatechange expects to be assigned with a function object and not the result/return value of a function call. The difference is the () after the function name!
2. You don't need to have the function call to getMessage() in the getMessage.php. Just call it inside the onreadystatechange callback of your code
3. The first parameter of setTimeout should also be a function object and not a string literal (although both work most of the time)
4. You should only start a new request after the old one finished. So put the window.setTimeout into the onreadystatechange callback for readyState==4


mTimer = window.setTimeout(getMessage, 2000);

5. This line should not be where it is right now


document.getElementById('new_messages').innerHTML = receiveReq.responseXML;

6. Question: Is your response really an XML document or rather HTML?
7. Where do you call getMessage() for the first time?

helloise
08-23-2011, 03:39 PM
thanks alot i will have a loot at all this...not that i understand it all quite :(

1: i have taken out the (): receiveReq.onreadystatechange = handleReceiveMessage;
2: dont understand?
6: i call: receiveReq.open("POST", 'newMessageCheck', true); and in 'newMessageCheck i just have an <?php echo "something" ?>
7: i changed it to the above..more descriptive and i only call it in the line above

devnull69
08-23-2011, 04:12 PM
thanks alot i will have a loot at all this...not that i understand it all quite :(

1: i have taken out the (): receiveReq.onreadystatechange = handleReceiveMessage;
2: dont understand?
6: i call: receiveReq.open("POST", 'newMessageCheck', true); and in 'newMessageCheck i just have an <?php echo "something" ?>
7: i changed it to the above..more descriptive and i only call it in the line above

So, my status for now

1. OK
2.+3.+4. In your original posting in the first code block there is one line of PHP. I assumed this to be coming from the getMessage.php because this seemed to be a logical conclusion. But you didn't tell us where this line is from ... so if it is from getMessage.php (which is the PHP you are requesting via Ajax) then you try to output the call to getMessage() there. You should take this out from the getMessage.php and call getMessage() instead at the end of your onreadystatechange callback. Something like


function handleReceiveMessage()
{
if (receiveReq.readyState == 4)
{
var chat_div = document.getElementById('div_chat');
var xmldoc = receiveReq.responseXML;
var message_nodes = xmldoc.getElementsByTagName("message");
mTimer = window.setTimeout(getMessage, 2000);
}
}

5.+6. So you just output some text or HTML stuff? Then you most definitely don't have an XML document (and you don't need one). But in that case your output will be available in receiveReq.resonseText rather than receiveReq.responseXML
7. Please change your code accordingly and post the full(!) code again. Make sure you post different files into different code blocks and mark the blocks accordingly (so that we know where the information is coming from)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum