473,769 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="anchornam e">...</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.getEl ementsByName && document.getEle mentsByName(s))
o = document.getEle mentsByName(s);
else if (document.all && document.all[s])
o = document.all[s];
else if (document.ancho rs && document.anchor s.length
&& document.anchor s[0].y) {
for (var i=0; i<document.anch ors.length; i++)
if (document.ancho rs[i].name==s) o = document.anchor s[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.locati on.hash.substr( 1);
ypos = getAnchorYPos(a nchorname); // 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 getElementsByNa me(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.anchor s is never null, but document.anchor s[0] and
document.anchor s[s] have no properties.
2. Should I use getElementByID? document.getEle mentByID is never null,
but document.getEle mentByID(s) is.

What am I missing?

-Alex
Jun 13 '06 #1
3 4083
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="anchornam e">...</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.getEl ementsByName && document.getEle mentsByName(s))
o = document.getEle mentsByName(s);


getElementsByNa me returns a collection, not a single element. Try:

if ( document.getEle mentsByName
&& document.getEle mentsByName(s). length)
o = document.getEle mentsByName(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************ **********@u72g 2000cwu.googleg roups.com>,
RobG <rg***@iinet.ne t.au> wrote:
getElementsByN ame returns a collection, not a single element. Try:

if ( document.getEle mentsByName
&& document.getEle mentsByName(s). length)
o = document.getEle mentsByName(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**********@b lue.rahul.net>, axlq <ax**@spamcop.n et> wrote:
In article <11************ **********@u72g 2000cwu.googleg roups.com>,
RobG <rg***@iinet.ne t.au> wrote:
getElementsBy Name returns a collection, not a single element. Try:

if ( document.getEle mentsByName
&& document.getEle mentsByName(s). length)
o = document.getEle mentsByName(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, getElementsByNa me(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
2941
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
1862
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 the images. Rather than just swap the images I would like t have it insert the image and move the other images down one position (or up) What would be even better is to have a radio button to specif whether you want to swap or insert the image. ...
4
548
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 hashFunction(char *key, int elements) ), so the table is an array of dynamic lists. The version of the program I've attached to this message works fine; but it's the result of an accurate debugging. I discovered that my problems consisted in just 4 lines...
13
1859
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 has_char(const char *, const char);
6
13584
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 > -1) { dg.Select(dg.CurrentRowIndex);
16
14503
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
1467
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 vb6 document and convert it into a vb.net program. Once I did that, the routine "check_for_file" I yanked out & put into my service program. The program though - doesn't work like the existing VB6 one does.
2
5010
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 page will return to the last position it had.) Is there any "easy" way to accomplish this? I've been using window.opener.location.reload();
3
1760
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 ******************************************************************************* Welcome to PeopleSoft 2 MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it a Enter the student number MSXMIC001 Enter the name
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9857
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
8867
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
6662
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
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
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
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
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.