1. Home
  2. Computing & Technology
  3. JavaScript

The Browser Object Model

Scroll Position

More useful BOM values that we can retrieve are those that give us the current scroll position of the web page within the browser. Again the way that the different browsers provide us with this information is different for the different browsers and again it is Internet Explorer that is the odd one out.

Here are a couple of functions that will allow us to retrieve the coordinates from whichever fields the browser stores the values in.

function scrollX() {return window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;}
function scrollY() {return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;}

Scrolling the current page to specific coordinates is much easier than it is to retrieve the coordinates of the current scroll position and all browsers do provide the same BOM method for performing this task.

window.scrollTo(posX, posY);
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.