I have a page which has a Div on it which gets constantly updated (every 1-3 seconds) but I want it to maintain its position between refreshes. I'm very very very new to Javascript and I am finding it very difficult to convert code that I've found online.

Can someone please point me in the right direction? So far the only code I've been able to find requires the code to make the div itself rather than modify an existing div.



Recommended Answers

All 4 Replies

give some id to the div. And by using its id change the content of the div.


<div id="mydiv"></div>


var mydiv = document.getElementById("mydiv");
mydiv.innerHTML = "Any content that is to be updated.";

If this is not what u were expecting then explain ur problem in more details.

Sorry, I really didn't explain myself very well.

I'm making a chat style system with the newest messages at the bottom. At the moment every time the content is refreshed the div automatically scrolls to the bottom (which is what I want) but, if the user is scrolling up and reading a previous message then it also jumps to the bottom. Obviously it would be better if, when people are scrolling and the page refreshes, it maintains the position of the scroll on the div.

Is that any clearer? I'm not sure..

if only the div content have to be changed then why u r refreshing whole page(u can use ajax). And to maintain the position while new content is added use textarea instead of div.

what say?

Na, you can just measure. If you use ajax instead of refreshing the whole page, you will have a chance right after the ajaz call is made successfully to check the position of the scroll bar. If it's all the way at the bottom, then add the text, and push the scroll even farther. If it's not at the bottom, then the user is scrolling, so update the text but don't move the scroll bar.

Be a part of the DaniWeb community

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