bigbinc wrote:
set scroll position in javascript.
Is there a way to do this without using document.location, example.
document.location = "#gohere"
<a name="gohere">
Using a function taking the name of an anchor:
function pageXY( el)
{
var XY={x:0, y:0};
for( var node = el; node; node=node.offsetParent)
{ XY.x += node.offsetLeft;
XY.y += node.offsetTop;
}
return XY;
}
function gotoName( name)
{
var anchors, anchor, XY;
anchors=document.anchors;
anchor=anchors[name];
if(!anchor) // IE sucks
{ for( var i = 0; i < anchors.length; ++i)
{ if(anchors[i].name==name)
{ anchor = anchors[i];
break;
}
}
}
if(!anchor)
{ if( document.getElementById)
anchor=document.getElementById(name);
else if( document.all) // untested
anchor=document.all[name];
}
if(anchor)
{ XY = pageXY(anchor);
window.scrollTo(XY.x, XY.y);
}
}
----
This contains some "paranoid" code in that it searches for an element
with the same id as the supplied name if it cannot find a matching
anchor, and makes no attempt to fix broken positioned offset chains as
may be encountered for anchors within tables using padding and borders,
or anchors within relatively positioned elements under MSIE.
Found in testing:
* IE 6 doesn't appear to implement document.anchors as an HTMLCollection
and didn't return a named anchor property,
* As I understand it, document.getElementsByTagName("A") should return a
nodeList without named lookup (part of the NamedNodeMap and
HTMLCollection interfaces). Not included in above code, Mozilla returned
an HTMLCollection, Opera 7 a "nodeList" with named lookup support and
IE6 some kind of index collection *with* named lookup.
For trivial comment, document.location was deprecated in favor of
window.location as far back as javascript 1.3, although all three
browsers tested seem to support it.
HTH
Dom