472,992 Members | 3,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

My code to get Y position of anchor - always null?

I wrote the function below to get the vertical scroll position of an
anchor. That is, a URL of the form
http://www.example.com/mypage.html#anchorname
should scroll to the point on the page that has an anchor
<a name="anchorname">...</a>).

Doing this in Javascript is necessary in the presence of a dynamic
HTML page having some hidden blocks. DHTML messes up the scroll
position in Opera and Mozilla (but not IE) so I'm trying to fix
it with javascript. Can anyone tell me why this function always
returns zero? What am I doing wrong?

=====================================
function getAnchorYPos(s) { /* s = string name of anchor */
/* get anchor object o */
var o=null;
if (document.getElementsByName && document.getElementsByName(s))
o = document.getElementsByName(s);
else if (document.all && document.all[s])
o = document.all[s];
else if (document.anchors && document.anchors.length
&& document.anchors[0].y) {
for (var i=0; i<document.anchors.length; i++)
if (document.anchors[i].name==s) o = document.anchors[i];
}
if (o==null) return null;

/* get anchor object Y position */
var ypos = 0;
var offset_parent = null;
offset_parent = o.offsetParent;
var el = o;
while (el.parentNode != null) {
el = el.parentNode;
if (el == offset_parent) {
ypos += o.offsetTop;
if (el.clientTop && el.nodeName != "TABLE") ypos += el.clientTop;
o = el;
if (o.offsetParent==null && o.offsetTop) ypos += o.offsetTop;
offset_parent = o.offsetParent;
}
}
return ypos;
}

var anchorname = document.location.hash.substr(1);
ypos = getAnchorYPos(anchorname); // ALWAYS ZERO ?!
=====================================

What I've observed:

1. The var anchorname has the correct value from the document URL.
2. The object o gets set by getElementsByName(s), however ALL of
the properties of o seem to be undefined after that.
3. document.all is never null, but document.all[s] has no properties.
4. document.anchors is never null, but document.anchors[0] and
document.anchors[s] have no properties.
2. Should I use getElementByID? document.getElementByID is never null,
but document.getElementByID(s) is.

What am I missing?

-Alex
Jun 13 '06 #1
3 4026
axlq wrote:
I wrote the function below to get the vertical scroll position of an
anchor. That is, a URL of the form
http://www.example.com/mypage.html#anchorname
should scroll to the point on the page that has an anchor
<a name="anchorname">...</a>).

Doing this in Javascript is necessary in the presence of a dynamic
HTML page having some hidden blocks. DHTML messes up the scroll
position in Opera and Mozilla (but not IE) so I'm trying to fix
it with javascript. Can anyone tell me why this function always
returns zero? What am I doing wrong?

=====================================
function getAnchorYPos(s) { /* s = string name of anchor */
/* get anchor object o */
var o=null;
if (document.getElementsByName && document.getElementsByName(s))
o = document.getElementsByName(s);


getElementsByName returns a collection, not a single element. Try:

if ( document.getElementsByName
&& document.getElementsByName(s).length)
o = document.getElementsByName(s)[0];

which will set the value of 'o' as a reference to the first element in
the collection (hopefully that's the one you want).

Or give it an ID and use getElementById, which returns a reference to a
single element.

[...]

I haven't tested the rest of your code...
--
Rob

Jun 14 '06 #2
In article <11**********************@u72g2000cwu.googlegroups .com>,
RobG <rg***@iinet.net.au> wrote:
getElementsByName returns a collection, not a single element. Try:

if ( document.getElementsByName
&& document.getElementsByName(s).length)
o = document.getElementsByName(s)[0];

which will set the value of 'o' as a reference to the first element in
the collection (hopefully that's the one you want).
Hmm... it returns null. And I know the argument (s) is the name of an
element of the form <a name="FL" ...>, and s="FL";
Or give it an ID and use getElementById, which returns a reference to a
single element.


Strange... that comes out null too, for <a name="FL" id="FL" ...>.
Funny thing is, *other* code using getElementById on the exact same
page (to hide a bunch of other elements in the un-expanded list)
work just fime.

-A
Jun 15 '06 #3
In article <e6**********@blue.rahul.net>, axlq <ax**@spamcop.net> wrote:
In article <11**********************@u72g2000cwu.googlegroups .com>,
RobG <rg***@iinet.net.au> wrote:
getElementsByName returns a collection, not a single element. Try:

if ( document.getElementsByName
&& document.getElementsByName(s).length)
o = document.getElementsByName(s)[0];

which will set the value of 'o' as a reference to the first element in
the collection (hopefully that's the one you want).


Hmm... it returns null. And I know the argument (s) is the name of an
element of the form <a name="FL" ...>, and s="FL";
Or give it an ID and use getElementById, which returns a reference to a
single element.


Ignore my last message. That worked. I was testing for the wrong
ID. Oddly, getElementsByName(s)[0] always returns null, when
testing with an element name that I know exists. Anyway, I have it
working now.

Thanks!!

-A
Jun 15 '06 #4

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

Similar topics

1
by: terry | last post by:
Hi, Could anyone teach me how to set anchor for a php link? Is the following right? www.test.com?abc=1#anchor_name Or the following? www.test.com#anchor_name?abc=1
1
by: portraitmaker | last post by:
I found some drag and drop code on the web and modified it a little b taking out some of the stuff I didn't need. This sample allows you to drag an image in a table to another positio and swaps...
4
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int...
13
by: Eric Lilja | last post by:
Hello, consider the following complete program: #include <assert.h> #include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> static int...
6
by: aaa | last post by:
Hi I am trying to create a read-only DataGrid that would always have current row selected. Currently, I am using method: public void SelectDataGridRow(DataGrid dg) { if (dg.CurrentRowIndex >...
16
by: Frances | last post by:
<a href="#1"> <a name="#1"> this link is not working in FF (works fine in IE..) would appreciate thoughts/suggestions.. thank you.. Frances
5
by: Blasting Cap | last post by:
I have a VB6 program that runs as an app, that I want to both convert it to vb.net, and run it as a service on the server. I have been able to create the service, and what I did was to open the...
2
by: Brent | last post by:
I have a child page opened with a simple open() statement. I'd like to be able to refresh the parent page and keep the current scroll position. (If I do an "F5" refresh on IE, for instance, the...
3
by: BlueroY | last post by:
hi, I'm working on an exercise, i did a lot of work already and i just can't figure where I'm going wrong, this is what I'm trying to achieve Sample IO...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.