javascript – How to get guaranteed correct page height? documentElement.offsetHeight and getBoundingClientRect () do not always work correctly

Question:

Faced the problem:

On some pages, document.documentElement .offsetHeight and .getBoundingClientRect() do not return the full page height, but actually the client side of the window.

When the window is resized, the specified indicators change.

Tried body instead of documentElement – same thing.

At the same time, there are elements that, according to getBoundingClientRect have a larger size.

Firefox browser.

How can the problem be solved?

Answer:

As always, as soon as I ask a question online, the answer is found by itself …

You need to use document.documentElement.scrollHeight , or it is better to choose the maximum indicator for reliability:

Math.max(document.body.scrollHeight,
  document.documentElement.scrollHeight, document.body.offsetHeight,
  document.documentElement.offsetHeight, document.body.clientHeight,
  document.documentElement.clientHeight
)
Scroll to Top