Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(window).height() vs $(document).height

Tags:

jquery

I am having problem of getting wrong height with

$(window).height();

and got the similar question here

In my case when I try

$(document).height();

it seems to return me correct result

window height returns 320

while document height returns 3552!

I found this question too

But in my case window already gets loaded completely as I am calling height function after few ajax operations

So what is the best way to know the height of the current window?

Edit:

enter image description hereenter image description here

like image 828
Pawan Nogariya Avatar asked Dec 26 '12 03:12

Pawan Nogariya


4 Answers

Well you seem to have mistaken them both for what they do.

$(window).height() gets you an unit-less pixel value of the height of the (browser) window aka viewport. With respect to the web browsers the viewport here is visible portion of the canvas(which often is smaller than the document being rendered).

$(document).height() returns an unit-less pixel value of the height of the document being rendered. However, if the actual document’s body height is less than the viewport height then it will return the viewport height instead.

Hope that clears things a little.

like image 113
Sayan Avatar answered Nov 16 '22 09:11

Sayan


This fixed me

var width = window.innerWidth;
var height = window.innerHeight;
like image 38
Ashraf Bin Akbar Avatar answered Nov 16 '22 08:11

Ashraf Bin Akbar


AFAIK $(window).height(); returns the height of your window and $(document).height(); returns the height of your document

like image 5
gamehelp16 Avatar answered Nov 16 '22 08:11

gamehelp16


jQuery $(window).height(); or $(window).width(); is only work perfectly when your html page doctype is html

<!DOCTYPE html>
<html lang="en">
...
like image 2
MRRaja Avatar answered Nov 16 '22 09:11

MRRaja