JavaScript » Window » scrollTo

window.scrollTo(xPosition, yPosition)

This method scrolls the contents of a window, the specified co-ordinate becoming the top left corner of the viewable area.

Both parameters are integers and they represent the x and y co-ordinates in pixels. This method is only useful where there are areas of the document not viewable within the current viewable area of the window and the visible property of the window's scrollbar must be set to true (enabled).


<script type="" language="JavaScript">

function toTop() {
        self.scrollTo(0, 0)


<form action="" method="POST" id="myForm">
<input type="Button" name="" value="Top" id="myButton" onClick=toTop()>

This example assigns the toTop() function to the onClick event handler of a button. Note that for this example to have any noticable effect, the button must be placed below the bottom of the default viewable area of the window, i.e. you have to scroll down to be able to see the button.