Table of contents problem (iframe linking) fixed

by D. C. Toedt on November 11, 2008

Yesterday I was able to fix the Javascript code that prevented the left-side table of contents from working properly in the Safari and Google Chrome browsers. I tweaked a solution suggested in an online comment (#7) by “inimino” at Targeting an anchor within iFrame; his(?) original code is in the source of this page.

function scrollToFrag(frag){
// Called by clicking on a link in the left-side table of contents in the main page.
// The argument ‘frag’ is the element identifier of a paragraph in the agreement draft,
// which is located is is in an iframe (‘ContentFrame’) on the right of the main page.

  objTemp=top.frames["ContentFrame"].document.getElementById(frag);
  objTemp.style.display=’block’;
  tmp = getPos(objTemp);
    // getPos is a routine that determines where in the document
    // the ‘frag’ paragraph is located
  top.frames["ContentFrame"].scrollTo(0,tmp);
    // I had to prepend the ‘top.frames["ContentFrame"].’ part
    // to make it work in Safari and Google Chrome.
    // The original code is more succinct; I expanded it
    // for debugging purposes.
}

function getPos(e) {
  var y=0;
  do{
    var yd = e['offsetTop'];
    y += isNaN(yd)?0:yd;
    e = e.offsetParent;
    } while (e && e!=document.body);
  return y;
  }

Share and Enjoy:
  • Facebook
  • Twitter
  • HackerNews
  • LinkedIn
  • del.icio.us
  • RSS
  • Add to favorites
  • Digg
  • Google Bookmarks
  • Posterous
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
Subscribe to the RSS feed, or sign up for email updates at right.



See also: (list is automatically generated)

Leave a Comment

Previous post:

Next post: