Why were window.scrollY and window.scrollX introduced?

17,243

Solution 1

Is there a browser which implements scrollY/X but doesn't support pageY/XOffset

I guess what you want to know is whether you can fully trust pageY/XOffset and leave scrollY/X out of the game. The answer is yes. pageY/XOffset is working in Firefox, Chrome, Opera, and IE 9!

I can't test scrollX/Y on IE9 currently, but it is not listed on MSDN properties so there is a good chance it answers your question. So there may be browsers implementing pageY/XOffset but not scrollY/X.

Why were window.scrollY and window.scrollX introduced?

As scrollY is only an alias, I'm sure it is only for better readability.

Solution 2

Everything about it. As you can see pageXOffset isn't supported by Internet Explorer 8 and below. In IE8 (and lower) you should use scrollLeft / scrollTop of document.body or document.documentElement depending what works (just like it is implemented in jQuery).

You can check it here: http://jsfiddle.net/8RFAn/1/

And I don't know why window.scrollX/Y were introduced, for me these properties looks like pageX/YOffset in terms of effect and browsers in which are implemented.

Share:
17,243
Knu
Author by

Knu

Updated on June 13, 2022

Comments

  • Knu
    Knu about 2 years

    As far as I know, pageXOffset/pageYOffset properties were already available since Netscape 4 era.
    And it seems scrollX/scrollY were introduced circa Netscape 6.

    Alternative question:

    Q2. Is there a browser which implements scrollX/scrollY but doesn't support pageXOffset/pageYOffset?

    I will add a third question because no one was able to answer the previous ones:

    Q3. scrollX/scrollY was added to the latest editor's draft of the CCSOM and the working draft only got pageXOffset/pageYOffset, why are they keeping both attributes?

  • Knu
    Knu over 13 years
    thx for the effort but it doesn't answer either of my questions ^^
  • Azmisov
    Azmisov over 13 years
    I speak of most of MSDN's properties.
  • Michael
    Michael almost 10 years
    Nope, according to the link you provided, pageY/XOffset is an alias to scrollX/Y developer.mozilla.org/en/DOM/window.scrollY
  • Dan Dascalescu
    Dan Dascalescu almost 9 years
    For cross-browser compatibility, use window.pageYOffset instead of window.scrollY.
  • John Harding
    John Harding over 7 years
    pageXOffset and pageYOffset are both IE9+
  • pepkin88
    pepkin88 over 7 years
    @JohnHarding Yes, but at the time of writing that answer IE9 didn't exist yet, at least not in a stable version.
  • John Harding
    John Harding over 7 years
    @pepkin88 makes sense, but could be confusing to users finding this page now.
  • pepkin88
    pepkin88 over 7 years
    @JohnHarding Fair enough, I updated the answer to explicitly show the versions I was referring to.