Height Of Page In Javascript
Solution 1:
Using jQuery (which you did specify), $(document).height()
will return exactly what you're asking for.
To clarify the usage of the height()
method:
$('.someElement').height(); // returns the calculated pixel height of the element(s)
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document
I suspect, that if $(document).height()
is not working for you, something is wrong. You may be:
- Calling it too early. Like, before the DOM is ready
- Have some uncleared floats that are not causing some block level elements to expand to their real height. Thus messing up height calculations.
- Have something critical absolutely positioned. Absolutely positioned elements do not contribute towards height calculations of their parent elements.
Solution 2:
If you're cool with using jQuery, what about getting the body or html height? like:
var winHeight = $('body').height();
Solution 3:
I was looking for this and landed up here. Without Jquery, you can get this with plain JS also. Below works:
console.log(document.body.clientHeight,document.body.scrollHeight,document.body.offsetHeight)
Do note the below link to clarify which to use: Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively
Solution 4:
I think you need to add window.pageYOffset
(the amount the page has been scrolled, in pixels).
Solution 5:
Not sure about JQuery. But this link, might help you to understand various properties and how they behave in different modes in different browsers.
http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
Post a Comment for "Height Of Page In Javascript"