Home > Javascript > Handling bookmarks / named anchors in an IFRAME

Handling bookmarks / named anchors in an IFRAME

February 14th, 2008

So, you are using IFRAMES, good on you. You have a nice document with lots of names anchors, but you find when you click on the bookmark link, the whole window, parent and all, scrolls into view. Pain in the ass. To get around it, use Javascript as such …

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
    while(1)
    {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)break;
        obj = obj.offsetParent;
    }
  else if(obj.y)
    curtop += obj.y;
  return curtop;
}
 
 
function scroll(element){
  p = findPosY(document.getElementById(element));
  window.scrollBy(0,p);
}

The findPosY is not my code, sorry, cant remember the original author - if I find you I will credit you.

In your HTML use

<a href="javascript:scroll('A');">A

and for the named anchor use any tag and add the ID for the link, ie

<h3 id="A">A

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Javascript

  1. No comments yet.
  1. No trackbacks yet.