How to move to an anchor specified in URI?

A

axlq

I'm trying to figure out how to make javascript set the scroll
position of a page to an anchor that's specified in the page URL.

You may ask, why? Doesn't the browser go there anyway when the URL
is loaded?

Yes, well, sort of. Dynamic HTML javascript activity that must
occur when the page loads messes up the scroll position in all
browsers except IE. (I'm contracting a bunch of expanded lists so
that javascript enabled browsers show a compressed clickable list,
and non-javascript browsers show a fully expanded list.) So I need
a way to re-set the position to the anchor specified in the URL.

I tried:

document.location.hash = location.hash;
or
window.location.hash = location.hash;

....but these result in the page freezing up before it finishes
loading -- which makes sense I guess (setting an object equal to
itself).

I also tried:

var hl = document.location.hash.substr(1,document.location.hash.length);
...[DHTML javascript goes here]...
document.getElementById(hl).focus();

....but this doesn't appear to have any effect. What should I be doing?

-Alex
 
J

josef.stich

Hi!

You can try to get the position of the Element that you want to scroll
to. (I can't find the link to the script that does that at the moment,
you have to fiddle with obj.offsetHeight)

Then you can use window.scrollTo() to go to this position.

HTH Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top