最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get window height? - Stack Overflow

programmeradmin1浏览0评论

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>

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>
Share Improve this question asked May 8, 2014 at 15:51 panthropanthro 24.1k70 gold badges200 silver badges349 bronze badges 5
  • 1 You need clientHeight – marekful Commented May 8, 2014 at 15:52
  • 2 A tale of two viewports at Quirksmode.org is a good read. – Greg Burghardt Commented May 8, 2014 at 15:54
  • @GregBurghardt Great link, but the OP understands those concepts, jQuery.height() is supposed to correctly get the height of browser window, not the HTML document. – Ruan Mendes Commented May 8, 2014 at 16:18
  • fyi, incorrect statement in user's question regarding window height: window height is not getting the length of the html document. A document can be larger than its window. – Govind Rai Commented Jan 4, 2017 at 17:58
  • Even though the answer below is correct, I feel as if OP wanted the size of the whole document. If that also fits your needs, you need to use document.body.clientHeight (as recommended by @marekful) if you want to calculate the height of the whole document. – Govind Rai Commented Jan 4, 2017 at 18:06
Add a comment  | 

2 Answers 2

Reset to default 16

window.innerHeight

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

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
发布评论

评论列表(0)

  1. 暂无评论