473,513 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to move to an anchor specified in URI?


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
Jun 12 '06 #1
1 5376
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
axlq schrieb:
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


Jun 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4203
by: Daniel Goldman | last post by:
When I open a new popup window, with a url that includes an anchor, about 1/5 of the time the page displays correctly (jumps to anchor), 4/5 of the time page displays incorrectly (starts from...
6
6169
by: Michael Hamm | last post by:
Hi, I'm trying to write a <form> whihc will retrieve a Web page on another server. I have (essentially) this: <form action="http://cgi.cs.indiana.edu/~oracle/digest.cgi"> <input...
9
8780
by: David Ross | last post by:
In some cases, I indicate links with both a small image and also text, both in the same anchor. When I upgrade my pages to incluce DOCTYPE for HTML 4.01 transitional, the underline for the link...
4
3437
by: deko | last post by:
I use named anchors to take users to specific parts of a long page. But I want to add some processing and do some things with my nav bar when users go to certain sections delineated by named...
3
4479
by: Jacob Thastrup | last post by:
Hi This is propably easy to answer but how do I make sure that my controls stay in a certain area of my form. Right now if I maximize my form the controls will stay in their original position and...
3
4507
by: Joseph Gruber | last post by:
Hi all -- I have two questions. First, I'm adding a control to my form at runtime and the control/form seems to ignore the anchor property of the runtime control. Any idea how I can get a...
3
5435
by: jaeden99 | last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with...
9
15164
by: paitoon | last post by:
Hello , My questionis short but i think many people got the same problem with me.. Is it possible to use the anchor tag in ajax ? I want the ajax load the page with specified area by using anchor...
21
9098
by: Prisoner at War | last post by:
Hi, People, Is it possible to have an "empty" or "dummy" <a href***without*** the browser jumping back up the page?? I have a hyperlink that doesn't point to another document, but is used to...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.