Categories
JavaScript Answers

How to detect document height change with JavaScript?

Spread the love

Sometimes, we want to detect document height change with JavaScript.

In this article, we’ll look at how to detect document height change with JavaScript.

How to detect document height change with JavaScript?

To detect document height change with JavaScript, we can use the resize observer.

For instance, we write

const resizeObserver = new ResizeObserver((entries) =>
  console.log(entries[0].target.clientHeight)
);

resizeObserver.observe(document.body);

to create a ResizeObserver object with a callback that runs when we resize the element being observed.

Then we call observer to watch for resizing of the body element with

resizeObserver.observe(document.body);

Conclusion

To detect document height change with JavaScript, we can use the resize observer.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.