A
axlq
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)
o = document.all;
else if (document.anchors && document.anchors.length
&& document.anchors[0].y) {
for (var i=0; i<document.anchors.length; i++)
if (document.anchors.name==s) o = document.anchors;
}
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 has no properties.
4. document.anchors is never null, but document.anchors[0] and
document.anchors have no properties.
2. Should I use getElementByID? document.getElementByID is never null,
but document.getElementByID(s) is.
What am I missing?
-Alex
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
o = document.all
else if (document.anchors && document.anchors.length
&& document.anchors[0].y) {
for (var i=0; i<document.anchors.length; i++)
if (document.anchors.name==s) o = document.anchors;
}
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
4. document.anchors is never null, but document.anchors[0] and
document.anchors
2. Should I use getElementByID? document.getElementByID is never null,
but document.getElementByID(s) is.
What am I missing?
-Alex