Skip to content

use of 'pageYOffset / pageXOffset' without 'window.' in 'coordinates' article #1599

Closed
@paroche

Description

@paroche

In the article: https://javascript.info/coordinates,

The code:

// get document coordinates of the element
function getCoords(elem) {
  let box = elem.getBoundingClientRect();

  return {
    top: box.top + pageYOffset,
    left: box.left + pageXOffset
  };
}

uses pageYOffset & pageXOffset without prefixing them with window., even though all previous discussions of the properties did use the window. prefix.

The window object was, I'm pretty sure, discussed in an earlier article, but I think it would be good to mention with this example, or, maybe better, in the previous article https://javascript.info/size-and-scroll-window#page-scroll how window properties are global variables, so that if one wants, window. can be omitted.

Additionally, in the interest of understanding code that one might encounter, perhaps a mention in the same place where window.pageYOffset et al are introduced that the window properties "scrollY" and "scrollX" are identical to "pageYOffset" and "pageXOffset" (though apparently they have less cross-browser compatibility, so are not recommended) would be good.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions