6

Are the following methods same?

window.scrollX  === window.pageXOffset

window.scrollY  === window.pageYOffset

If there is no difference, why two methods are introduced?

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

2 Answers2

10

From MDN:

The pageXOffset property is an alias for the scrollX property:

window.pageXOffset == window.scrollX; // always true 

For cross-browser compatibility, use window.pageXOffset instead of window.scrollX. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties.

Same thing goes for scrollY and pageYOffset.

Source: https://developer.mozilla.org/en-US/docs/Web/API/Window.scrollX

Wayne
  • 59,728
  • 15
  • 131
  • 126
  • It's not an exact duplicate, but you might also be interested in this question: http://stackoverflow.com/questions/3791336/why-were-window-scrolly-and-window-scrollx-introduced – Wayne Apr 23 '14 at 03:44
1

There is no difference between scrollX/scrollY and pageXOffset/pageYOffset. One is simply an alias of the other.

Mike
  • 4,071
  • 21
  • 36