Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    79
    Thanks
    7
    Thanked 0 Times in 0 Posts

    js not executing please help?

    i have this line:

    Code:
    echo $new_mail_cnt = '<script type="text/javascript">getMessages();</script>';
    then have:
    Code:
    <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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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
    Code:
    mTimer = window.setTimeout(getMessage, 2000);
    5. This line should not be where it is right now
    Code:
    document.getElementById('new_messages').innerHTML = receiveReq.responseXML;
    6. Question: Is your response really an XML document or rather HTML?

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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
    Code:
    mTimer = window.setTimeout(getMessage, 2000);
    5. This line should not be where it is right now
    Code:
    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?

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    79
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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
    Last edited by helloise; 08-23-2011 at 02:42 PM. Reason: more info

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Quote Originally Posted by helloise View Post
    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
    Code:
        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)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •