jQuery window height is not correct

jquery window height same as document heightI was working on a small jQuery snippet and got this weird issue. I was trying to get window height using jquery’s $(window).height() function.

Ideally $(window).height() returns the pixel less height of browser window. This is always the height of current browser window. If you resize browser this value should change.

Also you can get $(document).height(). $(document).height() returns an unit-less pixel value of the height of the document being rendered. If the actual document’s body height is less than the viewport height then it will return the viewport height instead. i.e. if you have less content in a page and window is sufficiently open to show this content then document height will be less than jquery window height.


However recently when I was playing with these values it seems both $(window).height() and $(document).height() gave me same value! Thus the $(window).height() was giving incorrect result.

Check out below source:


$(window).height() =
$(document).height() =

Lorem ipsum dolor sit amet, ...


What! The output shows value of $(window).height() and $(document).height() same 750. The window height is not equal. It should be 200px. Clearly we can see document is bigger, there is a scroll bar right there.

So why JQuery gives same value for window and document height? We’ll it turns out we missed a simple thing.


Our HTML document doesn’t have DOCTYPE declaration. So it’s not a valid HTML so to speak. JQuery cannot calculate window height / document height correctly if doctype is not specified on window.

The declaration is not an HTML tag; it is an instruction to the web browser about what version of HTML the page is written in.

So lets add DOCTYPE declaration to our HTML page and see the output.



Voila! It worked.. Never forget DOCTYPE declaration in your HTML. Always try to create HTML files using some kind of IDE. That will automatically take care of this.



  • Aziz Ali 1 April, 2014, 1:11

    Hey Viral,

    I was writing a small piece of code and I was facing he same trouble.
    window.innerHTML was giving the right value but $(window).height() was giving the document’s height.

    Your blog helped me figure it out :)

    BTW for virappatel.net blog readers. window.innerHTML: innerHTML is a property of the window object, natively available in all browsers. $(window).height() is ofcourse jquery pulling the same value.

  • venkat 3 April, 2014, 23:04

    Thank you sir,

  • Smoerrebroed 9 April, 2014, 18:11

    I still can’t believe it’s really that easy.
    You saved me a lot of time.

  • mikbert 18 April, 2014, 23:27

    This was driving me insane, as I would get the event for resize but the value for height was clearly wrong. Thank you!

  • Ed Chang 29 April, 2014, 7:42

    Thanks so much! Found this page through Google, and it fixed the bug I couldn’t solve.

  • John Ventimiglia 30 April, 2014, 4:01

    Did not work for me. Already had a DOCTYPE. Then I noticed I had output BEFORE the DOCTYPE.
    So seems FF but not IE need to have DOCTYPE BEFORE all else.
    That was driving me nuts because some of my pages worked, others did not.


  • S 3 May, 2014, 16:25


  • Ayrton 12 July, 2014, 1:31

    Thank you very much, this was exactly my problem! I was searching around for hours and nothing seemed to work until I found your solution! Great post!

  • Dimas 6 September, 2014, 16:28

    Thank you, your simple solution save me from my half day headache. Great post.

  • Wadim Grasza 8 September, 2014, 15:49

    OMG! This saved me so much time. Thanks for writing this.

  • Atott 10 October, 2014, 18:13

    Thanks for your solution!

  • H1ac0k 13 October, 2014, 20:41

    It works, thanks a lot…

  • awesome! 14 December, 2014, 21:59

    i dont usually leave commets on sites, but here i have to say that this saved me.

    Thank you!

  • Steve 19 December, 2014, 22:49

    Thank you, you saved me a lot of headache!

  • Alex 12 January, 2015, 17:57

    Thanks. You saved me a lot of debugging time!

  • Maulik 4 February, 2015, 15:05


  • avi 23 February, 2015, 23:58


Leave a Reply

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


To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]