Avatar of uhm179
uhm179
 asked on

Using Javascript to scroll to specific html element

Hi,

I am trying to implement unique URLs in my ajax application and so far so good.
Unfortunately, since the technique uses the fragment identifier '#' in the URL, I can no longer use it jumping to anchors.
(btw, The technique I'm using is actually just the new HistoryPoint feature in ASP.NET 3.5)

So I need to simulate the action of jumping to an anchor.
Lets say I have an element somewhere down the html page (a table cell for instance), and I have its id.
The browser obviously knows how to jump to an anchor, so how can i do it via javascript without actually using a fragment identifier.
(I've already tried to see whether i could retrieve the absolute pixel position of the element and then scroll to te appropriate place, but there is no such method for retrieving absolute pixel position).
JavaScriptAJAXASP.NET

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
mr_egyptian

You could use a function similar to the following.  Just pass an id.
  function jumpto(id){
      if((obj = document.getElementById(id)) && obj != null){
          window.scrollTo(0, obj.offsetTop);
      }
  }

Open in new window

ASKER CERTIFIED SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
uhm179

ASKER
@mr egyptian
Couldn't get your thingy to work. It just jumped to the top of the page. But I'm sure offsetTop will come in handy, once I get the hang of it.

@mplungian
Perfect. For those that want to use the function too, note that it works in IE, FF, Safari and Opera, BUT you must supply the boolean parameter, because for instance, Safari doesn't support the parameter-less version, according to this forum thread:

http://help.wugnet.com/bravenet/webmaster/replacement-scrollIntoView-method-Mozilla-ftopict33825.html

Thx
uhm179

ASKER
The parameter-less scrollintoview isn't supported by some browsers, buts otherwise, great.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Michel Plungjan

If perfect, then an "A" grade should be in order, no?
uhm179

ASKER
Well, I had to dig some stuff up myself about browser compatibility. Had you pointed out that there are browser compatibility issues and how to avoid them, I would've given an 'A' grade. Had I not checked myself just-in-case, it would have sent me on a wild bug-hunt.

So I only meant perfect in the sense that the scrollIntoView method was what I was looking for.
Michel Plungjan

'K
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.