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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *