<p>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</p>

<pre><code>container.innerHTML = content;
</code></pre>

<p>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.</p>

<p>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.</p>

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

<p>Does anybody have the idea how to deal with this?</p>

[{'answer_id': 63811, 'author': 'palehorse', 'author_id': 312, 'author_profile': 'https://Stackoverflow.com/users/312', 'pm_score': 0, 'selected': False, 'text': '<p>It sounds like you are having a problem with the browser itself.  Does this problem only occur in one browser?</p>\n\n<p>One thing you might try is using a lightweight library like <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a>.  It handles browser differences fairly nicely.  To set the inner HTML for a div with the ID of <strong>container</strong> you would simply write this:</p>\n\n<pre><code>$(\'#container\').html( content );\n</code></pre>\n\n<p>That will work in most browsers.  I do not know if it will fix your problem specifically or not but it may be worth a try.</p>\n'}, {'answer_id': 64219, 'author': 'Twan', 'author_id': 6702, 'author_profile': 'https://Stackoverflow.com/users/6702', 'pm_score': 0, 'selected': False, 'text': '<p>Would it work to set the scroll position back to the top (element.scrollTop = 0; element.scrollLeft = 0; by heart) before replacing the content?</p>\n'}, {'answer_id': 64596, 'author': 'jpbarto', 'author_id': 8511, 'author_profile': 'https://Stackoverflow.com/users/8511', 'pm_score': 1, 'selected': False, 'text': "<p>It sounds like the webkit rendering engine of Safari is not at first recognizing the content change, at least not fully enough to remove the previous html content.  Minimizing and then restoring the windows initiates a redraw event in the browser's rendering engine.  </p>\n\n<p>I think I would explore 2 avenues: first could I use an iframe instead of the current 'content' node?  Browsers expect IFrames to change, however as you're seeing they're not always so good at changing content of DIV or other elements.</p>\n\n<p>Secondly, perhaps by modifying the scroll position as suggested earlier.  You could simply move the scroll back to 0 as suggested or if that is to obtrusive you could try to restore the scroll after the content change.  Subtract the height of the old content node from the current scroll position (reseting the browser's scroll to the content node's 0), change the node content, then add the new node's height to the scroll position.</p>\n\n<p>Palehorse is right though (I can't vote his answer up at the moment - no points) an abstraction library like jQuery, Dojo, or even Prototype can often help with these matters.  Especially if you see your page / site moving beyond simple DOM manipulation you'll find the tools and enhancements provided by libraries to be a huge help.</p>\n"}, {'answer_id': 64736, 'author': 'Thevs', 'author_id': 8559, 'author_profile': 'https://Stackoverflow.com/users/8559', 'pm_score': 0, 'selected': False, 'text': "<p>Set the element's CSS height to 'auto' every time you update innerHTML.</p>\n"}, {'answer_id': 64740, 'author': 'Vitaly Sharovatov', 'author_id': 6647, 'author_profile': 'https://Stackoverflow.com/users/6647', 'pm_score': 0, 'selected': False, 'text': "<p>I would try doing container.innerHTML = ''; container.innerHTML = content; </p>\n"}, {'answer_id': 65912, 'author': 'Scott Swezey', 'author_id': 9439, 'author_profile': 'https://Stackoverflow.com/users/9439', 'pm_score': 3, 'selected': True, 'text': '<p>The easiest solution that I have found would be to place an anchor tag <code>&lt;a&gt;</code> at the top of the <code>div</code> you are editing:</p>\n\n<pre><code>&lt;a name="ajax-div"&gt;&lt;/a&gt;\n</code></pre>\n\n<p>Then when you change the content of the <code>div</code>, you can do this to have the browser jump to your anchor tag:</p>\n\n<pre><code>location.hash = \'ajax-div\';\n</code></pre>\n\n<p>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.</p>\n\n<p>(tested in the latest FF beta and latest safari)</p>\n'}]
['https://Stackoverflow.com/questions/63743', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3894/']