3
votes

jQuery documentation http://api.jquery.com/height/ says

// Returns height of browser viewport
$( window ).height();

// Returns height of HTML document
$( document ).height();

But I got the same value from both method. For example, (with html including overflowed div with height=about 3000px)

$( window ).height();
3588
$( document ).height();
3588
$("body").height();
3572
$("html").height();
3588
window.innerHeight;
667

It gave the same result both in Chrome and Firefox browser. (The value is slightly different because of different size of toolbar.)

What I expected for $( window ).height() was "height of browser viewport" which is window.innerHeight = 667. But it gave 3588 which is much bigger than I expected.

Did I understand something wrong? Does viewport mean something different?

Anyway, in Find the exact height and width of the viewport in a cross-browser way (no Prototype/jQuery) , the way to get the height and width of the viewport is explained. In this document, "the viewport" is what I think, but not the one explained in jQuery documentation.

===============================================

Lately editted:

It only gives the wrong answer 3588 when I open the offline html file in my computer. When I uploaded the html file to my blog, and tested it, it gives the expected correct answer 667. Is there any differnce between openning an offline file (file://) and an online html file (http://) ?

Short version of my test html file.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div style="height:3500px; background:rgb(200,200,255)" id="result"></div>

<script>
$("#result").html("$(window).height():? "+$(window).height());
</script>

It gives still

$(window).height():? 3516

in offline only.

1
Create a page that needs to be scrolled to view its contents. window is what you can see, document is the whole page. - Popnoodles
"window is what I can see". But why does $(window).height() give so big number (3588) bigger than my screen height? @Popnoodles - kipid
Was this running inside of an iframe or something? - James Montagne
I just run it in console and also tested with a html file. @JamesMontagne - kipid
Nope. unless you're using IE and it's going to compatibility mode. - Kevin B

1 Answers

0
votes

I am not sure whether it is a bug or an intended change in behavior, but what you describe does happen in the newer jquery releases. You can use window.innerHeight instead. Its is cross-browser (IE 6+)