The most common way to scroll a page is to link the user to a URL with #AnchorId at the end. This method allows the browser to handle everything and there is no code needed. For times when you need a solution using code I'm going to demonstrate two other methods with Javascript.
The first option is to use the built in Javascript function location.href. This function allows you to send the user to any URL or Anchor you'd like. Using this function we're going to send the user to an Anchor on the current page with an Id of MyAnchor.
Place an Anchor tag with the Id MyAnchor on the part of the page you'd like to scroll to. The text "scroll to here" is optional.
Code:<a id="MyAnchor">Scroll to here</a>
Although you can call the location.href function at anytime after the page is loaded, for this example we're going to call it right as the page loads. Place the code at the bottom of the BODY tag and right above </body>.
Code:<script language="javascript" type="text/javascript">
location.href = '#MyAnchor';
</script>
The second method is to scroll the user directly to an object. This can be almost any object and doesn't have to be an Anchor. It's useful when you don't have an Anchor in the area or simply don't want to put one there. The function below includes another function called ObjectPosition which can be found ---------------here-----------------.
Code:function ScrollTo(obj){
try{
var objpos = ObjectPosition(obj);
}catch(e){}
try{
scroll(0,objpos[1]);
}catch(e){}
try{
window.scrollTo(0,objpos[1]);
}catch(e){}
}
Obj is the object you'd like to scroll to.
The ObjectPosition returns both the left and top offset as a two item array. You'll notice that I hardcoded 0 in the two scroll functions. You can change that to objpos[0] if you need to scroll horizontally. I'm using two different scrolling methods because of browser issues. For those of you who think I'm misusing try/catch, too bad, it's a shortcut and I'm using it.