javascript position page

B

bigbinc

set scroll position in javascript.

Is there a way to do this without using document.location, example.

document.location = "#gohere"


<a name="gohere">
 
S

Stuart Palmer

just use an <a href> tag, no need for JS here

<a href="pagename.html#gohere">Link</a>

Stu
 
F

Fred Basset

In IE only, you can use the scrollIntoView method.

object.scrollIntoView()

You can also pass an argument to specify scrolling the element to the
top or bottom of the page. For more information, see ...

http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/scroll
intoview.asp

Not ideal if you are publishing to the internet, but if it's an intranet
or controlled environment then you could use it.

Fred Basset
(e-mail address removed)
 
D

Dom Leonard

bigbinc said:
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.name==name)
{ anchor = anchors;
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,082
Messages
2,570,587
Members
47,209
Latest member
Ingeborg61

Latest Threads

Top