panthro
panthro

Reputation: 24061

Get window height?

window height is getting the length of the html document rather than the size of my browser window.

Do you know where I'm going wrong?

Here's my script:

$(document).ready(function() {
            alert($( window ).height());
    });

I also have at the top of my doc which I've read can cause issues:

<!DOCTYPE html>

Upvotes: 7

Views: 25071

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92274

I think it must be an old version of jQuery that is buggy? Using 1.6.4 (the oldest that jsfiddle supports), I get the right behavior. See JSFiddle

If you can't figure it out, you can use window.innerHeight, I try to use jQuery only if it takes less code, which it doesn't in this case.

div {
  height: 2000px;
}
// onload
alert($(window).height());
// outputs window height, not 2000

Upvotes: -1

Donald Powell
Donald Powell

Reputation: 774

window.innerHeight

$(document).ready(function() {
     alert(window.innerHeight);
});

Upvotes: 16

Related Questions