View Full Version : anchor with iframe
dojjen69
04-18-2007, 07:11 PM
Heyas, I've been messing with a problem all day using any resources I can find on the net and nothing seems to work. Here's the problem...
I have a page (index.htm) with an iframe in it (main.htm). main.htm is a long page so I setup an anchor at the bottom of index.htm so when clicked, would take me to the top of index.htm. The problem is, it doesn't work right. For starters, when index.htm loads with main.htm, the page viewing automatically jumps to the beginning of main.htm which makes the header section of index.htm out of sight unless you scroll up.
With the anchor, the same thing happens. The page jumps to the top of index.htm then instantly jumps down to the beginning of main.htm.
Anyone have any thoughts on this? I've used anchors before but never run into this problem...
thanks for any help...
Veronica
04-19-2007, 01:31 PM
In iframes, named anchor links seem to affect the page containing the iframe, as well as the iframe.
Instead of using the "a name" tag, put this at the bottom of main. htm:
<a href="javascript:void()" onclick="window.scrollTo(0,0); return false">Back to Top</a>
This will scroll main.htm to the top of the page, without affecting index.htm
dojjen69
04-19-2007, 03:15 PM
That worked beautifully! Thank you!
One small problem though...
main.htm worked fine... it was getting index.htm to scroll to the top of it instead of the top of the iframe (main.htm) The java code worked for that, but when the parent loads with the iframe in it, browsers automatically scroll to the top of the iframe instead of the top of the parent. Is there anyway to to modify the java to fix this?
I tried this, but it didn't work...
<body onload="window.location.href='javascript:void()" onclick="window.scrollTo(0,0); return false'">
I'm guessing because of the "ONCLICK" parameter.
As well.. this just occured to me... is there anyway of having the "BACK TO TOP" link on the parent and having it scroll the iframe to the top and then scroll the parent to the top? It'd be cool if this was possible, but it's gravy... my main concern is fixing the initial problem.
Thanks so much for the help so far.
dojjen69
04-19-2007, 03:41 PM
I kind of figured something out... adding this to the bottom of the page seems to work... it makes the page jump when it loads, but it works.
<script> setTimeout("self.scrollTo(0,0)",1);</script>
Is there anything better than this?
and still... is there any way to modify this code...
<a href="javascript:void()" onclick="window.scrollTo(0,0); return false">Back to Top</a>
...to scroll the iframe to the top and then scroll the parent to the top?
Thanks again for all the help.
Veronica
04-19-2007, 03:49 PM
I'm not sure I understand what you're asking. If you want a link on index that would send main to the top, you would just put a regular link on index like this (no pound signs or anything):
<a href="main.htm" target="whatever_iframe_name_is">top</a>
That shouldn't make index move, it will just reload main to the top.
If you want a link to an "a name" in main, that would send index to the top, use:
<a href="javascript:void()" onclick="window.location.href='#top'; parent.scrollTo(0,0); return false">Back to Top</a>
That will send both index and main to the top.
dojjen69
04-19-2007, 03:55 PM
I was asking if there was a way to have a link on the index page that would scroll the iframe to the top and the parent to the top in one click... but actually I guess I haven't had enough coffee this morning... I forgot my iframe auto-adjusts for height so it never scrolls anyways.
Sorry about that... but I appreciate the reply anyways :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.