Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ScrollTop behaving unexpectedly in IE browser 1

Status
Not open for further replies.

rrjammy

Programmer
Joined
Feb 9, 2006
Messages
2
Location
US
A little help please...

I have a <div> (let's call it divName), and I want to store its scroll position in a hidden variable (let's call it nameCoOrd). I store its scroll position using:

Code:
document.formName.nameCoOrd.value = document.getElementById('divName').scrollTop;

This seems to work fine in both FireFox and IE.

Now, I do some changes to the <div> (using JavaScript), and I want to restore the scroll position, so I use

Code:
document.getElementById('divName').scrollTop = document.formName.nameCoOrd.value;

This seems to work properly in FireFox, but in IE, the <div> always scrolls to the position 214. However, if I give an alert statement like

Code:
alert(document.formName.nameCoOrd.value);

before reseting the scrollTop, it works properly. I am completely stumped by this problem, and any help will be greatly appreciated. Thanks in advance!

rrjammy.
 
Your problem sounds vaguely familiar but I am not sure. It might be a timing issue. The changes to the HTML might not be fully realized by the browser before the command to change scroll position is received causing confusion on placement.
Try testing by executing a timer delay immediately before changing the scrolltop position. If it works with the delay you can experiment with shorter delays until you know what works.

I had similar scroll problems in the past that were timing related but due to mouse behaviour I had setup.



Stamp out, eliminate and abolish redundancy!
 
Yep, it was a timing issue. Using a timeout of 0 seconds (!) solved it...

Code:
setTimeout("document.getElementById('divName').scrollTop = document.formName.nameCoOrd.value", 0);

Thanks for your help!!
 
You're welcome. It took me some time to figure out why my own code was behaving that way and I believe it was someone in these forums that gave me the answer. :)


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top