943,840 Members | Top Members by Rank

Ad:
Sep 15th, 2009
0

AJAX in IE malfunction

Expand Post »
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function retrieve_messages() {
  2. // Checks wether the object is available:
  3. if (ajax) {
  4. ajax.open('get', 'messageretrieve.php');
  5.  
  6. // Sends request
  7. ajax.send(null);
  8.  
  9. // Function that handles response
  10. ajax.onreadystatechange = setTimeout("show_messages()", 500);
  11.  
  12.  
  13. } else { // AJAX is not useable
  14. document.getElementById('warning').innerHTML = 'It is not possible to connect, please update your browser.';
  15. }
  16.  
  17. } // End of retrieve_messages()
  18. // Function that shows the returned text into the messagebox
  19. function show_messages() {
  20. // If everything is OK:
  21. if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
  22. // Returns the value to the document
  23. document.getElementById('messagebox').innerHTML = ajax.responseText;
  24. }
  25.  
  26. } // 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.
Last edited by Graphix; Sep 15th, 2009 at 3:59 pm.
Similar Threads
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
401 posts
since Aug 2009
Sep 16th, 2009
1

Re: AJAX in IE malfunction

Try,

javascript Syntax (Toggle Plain Text)
  1. 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
Last edited by Airshow; Sep 16th, 2009 at 7:55 am.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
Sep 17th, 2009
0

Re: AJAX in IE malfunction

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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <body onload="setInterval('retrieve_users()', 1500); setInterval('retrieve_messages()', 1500)" onunload="leave_chat()">
  2. ....
  3. .... not relevant HTML
  4. .....
  5. <form method="post" name="sendform" action="defaultchat.php">
  6. <input type='text' name='textbox' id='textbox' maxlength='255' size='75' /><input type='button' value='Send Message' onclick='send_message()' />
  7. </form>
Last edited by Graphix; Sep 17th, 2009 at 11:44 am.
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
401 posts
since Aug 2009
Sep 17th, 2009
0

Re: AJAX in IE malfunction

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 :
javascript Syntax (Toggle Plain Text)
  1. <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
Last edited by Airshow; Sep 17th, 2009 at 1:48 pm.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
Sep 19th, 2009
0

Re: AJAX in IE malfunction

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

Thread Solved

~Graphix
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
401 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Prototype Ajax error?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Prototyping an element





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC