How to find the maximum scroll position of a page with JavaScript?

Sometimes, we want to find the maximum scroll position of a page with JavaScript.

In this article, we’ll look at how to find the maximum scroll position of a page with JavaScript.

How to find the maximum scroll position of a page with JavaScript?

To find the maximum scroll position of a page with JavaScript, we can find the max value between document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, and document.documentElement.offsetHeight.

For instance, we write:

const limit = Math.max(
  document.body.scrollHeight,
  document.body.offsetHeight,
  document.documentElement.clientHeight,
  document.documentElement.scrollHeight,
  document.documentElement.offsetHeight
);
console.log(limit)

to call Math.max with the listed values to find the max scroll position of the page.

Conclusion

To find the maximum scroll position of a page with JavaScript, we can find the max value between document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, and document.documentElement.offsetHeight.