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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scrollTo not working in IE6 or Opera 1

Status
Not open for further replies.

Itshim

Programmer
Apr 6, 2004
277
US
OK, I'm stumped.

I am trying to use window.scrollTo(x, y) when after a page has finished loading. The code works fine in Netscape/Mozilla, but fails in Opera/IE6 (possibly other versions of IE, but I only have 6 to test on ATM).

I was obtaining the x/y coordinates dynamically, but since it was not working I have them hard-coded, for now. My code is:
Code:
function Scroll_To()
{
    window.scrollTo(0, 450);
}
I have verified that the function is being called.

I'm not sure if it matters but I have the DOCTYPE set to HTML 4.01 Transitional. The only reason I mention it is because the doctype was causing scrollTop to fail, but that has now been fixed :)

TIA,
Itshim
 
Your function works for me in both Opera and IE6 with NO DOCTYPE set.
Code:
<html>
<head><script>
function Scroll_To()
{
    window.scrollTo(0, 450);
}
</script></head>
<body onload='Scroll_To();'>
<pre>
Hello...


































































...World!</pre>
</body></html>

Then I added:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
to the top of the HTML... and it STILL worked.

So, I have to ask this... Is the page you're trying this on LARGER than 450 px vertically? If it's not, there shouldn't be ANY scrolling. If it is larger than 450, but not larger than 450+screen.height, then it should scroll, but not all the way to 450. In the second case, it would scroll until the bottom of the page is at the bottom of the screen.

Does this help at all?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Dave, thank you for the reply. Yes it did help, although I have not figured out the proper solution yet.

I misspoke in my post...in my page, Scroll_To() is being called before the entire page loads (which is what is screwing up IE). The page is part of a larger application and I'm using templates for different sections of the page. I have the Scroll_To() call at the bottom of the 'interface' template, but not at the bottom of the page. I cannot put an onLoad event in the body tag, because I only want it to scroll for certain pages. Basically I need to set an onLoad event in the middle of the page. I tried:
Code:
window.onLoad = Scroll_To();

But no luck yet...

Thank you for the help,
Itshim
 
Solved!!!

I just needed to write the code in the previous post correctly :)

The following code works in IE/Opera/Netscape/Mozilla:
Code:
window.onload = Scroll_To;

Thanks again,
Itshim
 
Great!

I make that typo with the parentheses a lot too. 'glad to see I'm not alone. :)

Thanks for the star.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top