Forum Moderators: open

Message Too Old, No Replies

div.innerHTML is forcing page to scroll to top?

innerHTML scrolling page

         

erikcw

10:04 pm on Jun 13, 2008 (gmt 0)

10+ Year Member



Hi all,

I'm using javascript to insert chunks of html into parts of my page. However, wen the function is fired, the page scrolls to the top (or near the top), so the user has to scroll back down to see their data.

I'm basically doing a


div.innerHTML="<div>...blah ... blah...</div>";

Any idea why this is making the browser scroll?

Thanks!

penders

4:29 pm on Jun 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How/when are you calling this function? As a result of some user action eg. clicking a link perhaps?

Assigning content to the innerHTML of an element should itself not make the page scroll.

erikcw

6:38 pm on Jun 15, 2008 (gmt 0)

10+ Year Member



I'm using an onclick


<a href="#" onclick="addcontent();return false;">blah</a>

At first I thought the "#" was doing it, even though I'm using "return false".

But then tried running addcontent() in firebug, and the scrolling still happens, so I don't think it is the onclick.

Seb7

7:03 pm on Jun 15, 2008 (gmt 0)

10+ Year Member



I've had to over come this problem before. The method I used was to add some java to scroll to the bottom straight after update, which works better than you think it would.

erikcw

10:38 pm on Jun 15, 2008 (gmt 0)

10+ Year Member



I wonder if that is the only workaround? In my application, it is a rather long page, and dynamic text can be added by the user with this function at any point in the page. So scrolling to the bottom seems like it would just reverse by problem (user would get bumped to the bottom of the page instead of the top...)

penders

6:31 am on Jun 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



At first I thought the "#" was doing it, even though I'm using "return false".

The '#' would cause this if it was followed, but like you say, the

return false
should prevent this. You could try using:
href="javascript:void(0);"

Can you paste your addcontent() code?

monica45

11:10 am on Jul 18, 2008 (gmt 0)

10+ Year Member



Have you found a solution, i have the same problem...

monica45

11:22 am on Jul 18, 2008 (gmt 0)

10+ Year Member



?

monica45

11:39 am on Jul 18, 2008 (gmt 0)

10+ Year Member



forget it, the ajax example that i use includes

arent.scroll(0,0)

AAAAARRRGGG!

kceleb9

1:48 am on Jul 20, 2008 (gmt 0)

10+ Year Member



Any solution to the original post? I agree there is no reason why adding content to the DOM should cause the window to scroll. erikcw Please paste your handler and you will probably have somebody spot the issue.

The only other thing that comes to mind for me immediately is focus - if you have an element you add to the div that contains a focusable element - and you shift focus there - you would get a scroll effect.