473,657 Members | 2,474 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.locati on.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.locati on.hash.substr( 1,document.loca tion.hash.lengt h);
...[DHTML javascript goes here]...
document.getEle mentById(hl).fo cus();

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

-Alex
Jun 12 '06 #1
1 5390
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.offsetHeigh t)

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.locati on.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.locati on.hash.substr( 1,document.loca tion.hash.lengt h);
...[DHTML javascript goes here]...
document.getEle mentById(hl).fo cus();

...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
4213
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 top). Here is the code I use: HelpWindow_j = window.open ('webpage.htm#uniqAnchor', 'uniqName', 'hotkeys,location,resizable,scrollbars,height=300,width=600', true); This happens in IE 6.0, not in NS 4.7; NS 4.7 works correctly. I have not tested...
6
6183
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 type="submit"><select name="N"> <option value="1234#1234-01">foo <option value="1010#1010-02">bar <option value="1001#1001-03">baz
9
8795
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 extends to the image. Without the DOCTYPE, the underline appears only for the text. Is there a way to specify a style that I can use with the DOCTYPE that will allow the underline for text in a link but suppress the underline for an image in...
4
3449
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 anchors. I understand that the anchors array creates an element for each anchor in the page when the page is loaded, but how do I identify which anchor is currently being viewed? Here's some pseudo code: if (document.anchors.name == "section_B") {
3
4486
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 the form will expand all around it, how can I make my controls stay, say in the bottom right corner no matter the size of the form and the resolution of the monitor? Thanks Jacob Thastrup
3
4515
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 runtime control to anchor properly? Second -- the program I'm writting is supposed to look like a dos application (long story). When the application is "Normal" aka a small window then everything looks great. But when I resize the window to...
3
5449
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 users if it's ok to delete. Thanks in advance. i found one that was really close but it only looks at one folder. I need it to look at files within folders and subfolders too. Dim oFSO, wshShell, FileCol, oFolder, objTextFile, shareLength,...
9
15175
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 tag...if yes how ? thank you so muc Alexander
21
9145
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 call forth a modal window onClick (or is there another way, without text or image links, of calling forth JavaScript on user activity??). I would like to spare my visitors the inconvenience and visually jarring effect of getting thrown back up to...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
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 we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.