AJAX PHP Chat load Iframe and scroll to the bottom


I have a fairly simple question, but I cannot seem to get it to work. I am creating a simple online chat application, and the html page for the chat contains a textbox to submit new messages. The html page also contains an iframe to another html page that dynamically loads the messages. The problem I am having is that I want the iframe to scroll to the bottom when the page is first loaded and when the user posts a new message. I also want to be able to detect when the scrollbar is at the bottom and when another user posts the scroll bar scrolls to the bottom displaying the new message. If you could help me out with this I'd really appreciate it. If you could show me how I might go about doing this and provide some code examples, I'd really appreciate it.

Here is a snippet of the html page that contains the iFrame:
<!-- Here is where the AJAX Javacript code is -->
<table width="100%"  border="0" cellpadding="3" cellspacing="4" bgcolor="#000000">
  <td bgcolor="#006699">Chat App</td>
<tr cellpadding="0" cellspacing="0">
<td bgcolor="#ffffff" cellpadding="0" cellspacing="0">
<div bgcolor="#ffffff">
<iframe frameBorder="0"
src ="get_messages.html"
  <form onsubmit="postMessage(textvalue.value)">
  <input type="text" id="textvalue"  />
  <input type="submit" value="snd"  />

Open in new window

Who is Participating?
Xyptilon2Connect With a Mentor Commented:
Fastest non-javascript way is to place an HTML anchor at the bottom of the page like <a name='anchor'></a> And post to whatever.php#anchor

You won't need javascript for that. Or if you want to use javascript:

if (document.body.scrollHeight) {
  window.scrollTo(0. document.body.scrollHeight);
else if (screen.height) { // IE5
  window.scrollTo(0. screen.height);

something like this should work
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.