Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > My code to get Y position of anchor - always null?

Reply
Thread Tools

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

 
 
axlq
Guest
Posts: n/a
 
      06-13-2006
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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-14-2006
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

 
Reply With Quote
 
 
 
 
axlq
Guest
Posts: n/a
 
      06-15-2006
In article <(E-Mail Removed) .com>,
RobG <(E-Mail Removed)> 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
 
Reply With Quote
 
axlq
Guest
Posts: n/a
 
      06-15-2006
In article <e6qnfr$eqk$(E-Mail Removed)>, axlq <(E-Mail Removed)> wrote:
>In article <(E-Mail Removed) .com>,
>RobG <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
position page fragment (anchor) on top of page. Erwin Moller HTML 1 07-27-2012 10:42 AM
Position absolute not always removed from flow? Nik Coughlin HTML 8 05-02-2008 07:40 PM
Tableless layout. Is it always divs with position:absolute? ThunderMusic ASP .Net 6 09-28-2006 02:06 PM
Where is Form Relative Position and Absolute Position in VS.Net 2005 ? Luqman ASP .Net 1 02-07-2006 10:27 AM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments