SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Darth Cow's Avatar
    Join Date
    Oct 2002
    Location
    Swarthmore, PA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Scrolling to an anchor in the top frame from an iframe

    Here's my function topScroll() that's on the page with the iframe, when code liberally taken from the recent SitePoint article on scrolling:

    Code:
      function topScroll()
      {
      	var anchor = "CommentsBox";
      	var allLinks = top.document.getElementsByTagName('a');
      	var destinationLink = null;
      	for (var i = 0; i < allLinks.length; i++)
      	{
      		var lnk = allLinks[i];
      		if (lnk.name && (lnk.name == anchor))
      		{
      			var desty = lnk.offsetTop; 
      			break;
      		}
      	}
      
      	top.window.scrollTo(0,desty);
      }
    However, when I call the function, the page scrolls to the top of the complete page instead of to the anchor. I'm certain the anchor is there and that the function is being called.

    Does anyone know what I'm doing wrong?

    Thanks for your help!

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Why not just use location.hash?
    Code:
    function topScroll()
    {
      top.location.hash = "CommentsBox";
    }

  3. #3
    SitePoint Enthusiast Darth Cow's Avatar
    Join Date
    Oct 2002
    Location
    Swarthmore, PA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. That is way too easy. Thanks!

  4. #4
    SitePoint Enthusiast Darth Cow's Avatar
    Join Date
    Oct 2002
    Location
    Swarthmore, PA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I have a followup question now. Is there some easy code I could use to automatically attack a trigger to that function to every <a> tag in the iframe?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •