Hi,

I am currently writing a chatprogram, and it works fine in FF but it doesnt refresh as it should every 1500ms (set with setInterval("retrieve_messages()",1500) in body tag) in IE. The object ajax is made in previous code.

I wrote the following code:

function retrieve_messages() {
    // Checks wether the object is available:
    if (ajax) {
        ajax.open('get', 'messageretrieve.php');

        // Sends request
        ajax.send(null);

        // Function that handles response
        ajax.onreadystatechange = setTimeout("show_messages()", 500);


    } else { // AJAX is not useable
        document.getElementById('warning').innerHTML = 'It is not possible to connect, please update your browser.';
    }

} // End of retrieve_messages()
// Function that shows the returned text into the messagebox
function show_messages() {
    // If everything is OK:
    if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
        // Returns the value to the document
        document.getElementById('messagebox').innerHTML = ajax.responseText;
	}

} // End of function show_messages()

IE says that there is something not implemented on line 9 (ajax.onreadystatechange=.....), could somebody help me out?

~Graphix

EDIT: I am currently going to bed, i will be back tomorrow.

Recommended Answers

All 4 Replies

Try,

ajax.onreadystatechange = function(){setTimeout("show_messages()", 500);}

It is also good practise to establish the response handler before sending the request, so put lines 9/10 above 6/7.

Airshow

commented: Good support, clear explaination. +1

Ok thank you, it worked :). But i also have another question, i have a small problem with my chat-program: if the users presses enter it will reload and it will log out due to the onunload="leave_chat()" part. But if i delete the form i cant referer to the text input and as so cant retrieve the value typed in the textbox.

Eg:

<body onload="setInterval('retrieve_users()', 1500); setInterval('retrieve_messages()', 1500)" onunload="leave_chat()">
....
.... not relevant HTML
.....
 <form method="post" name="sendform" action="defaultchat.php">
  <input type='text' name='textbox' id='textbox' maxlength='255' size='75' /><input type='button' value='Send Message' onclick='send_message()' />
 </form>

Graphix,

You need to suppress the form's natural tendency to want to be submitted when a field has focus and user hits enter. That's fairly simple to cure because the form (as far as I can see) never needs to be submitted. Two options immediately come to mind:

1. Reduce the <form ...> tag to <form>, and call send_message with onclick='send_message(this.form.textbox.value)' to make the user-entered text available as an argument to the function.

2. Leave the form tag in place but change it to :

<form name="sendform" onsubmit="return false;">

onsubmit="return false;" ensures that the form will not be submitted, whilst your button's onclick="send_message()" will still work.

Goood luck

Airshow

Thanks for help, I completed the chat-program and it works perfectly.

Thread Solved :)

~Graphix

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.