Sergey Mikhanov Sergey Mikhanov - 1 year ago 73
HTML Question

innerHTML manipulation in JavaScript

I am developing a web page code, which fetches dynamically the content from the server and then places this content to container nodes using something like

container.innerHTML = content;

Sometimes I have to overwrite some previous content in this node. This works fine, until it happens that previous content occupied more vertical space then a new one would occupy AND a user scrolled the page down -- scrolled more than new content would allow, provided its height.

In this case the page redraws incorrectly -- some artifacts of the old content remain. It works fine, and it is even possible to get rid of artifacts, by minimizing and restoring the browser (or force the window to be redrawn in an other way), however this does not seem very convenient.

I am testing this only under Safari (this is a iPhone-optimized website).

Does anybody have the idea how to deal with this?

Answer Source

The easiest solution that I have found would be to place an anchor tag <a> at the top of the div you are editing:

<a name="ajax-div"></a>

Then when you change the content of the div, you can do this to have the browser jump to your anchor tag:

location.hash = 'ajax-div';

Use this to make sure the user isn't scrolled down too far when you update the content and you shouldn't get the issue in the first place.

(tested in the latest FF beta and latest safari)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download