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

javascript - document and window height return the same value? - Stack Overflow

programmeradmin2浏览0评论

For some reason both document height and window height return the same value, so when subtracting window from document height it returns 0. Anyone know why this might be happening?

console.log($(window).height());

console.log($(document).height());

The above both return the document height

For some reason both document height and window height return the same value, so when subtracting window from document height it returns 0. Anyone know why this might be happening?

console.log($(window).height());

console.log($(document).height());

The above both return the document height

Share Improve this question edited Apr 21, 2013 at 13:09 Matthew Gatland 3241 gold badge3 silver badges13 bronze badges asked Aug 23, 2012 at 18:57 user1522379user1522379 1
  • possibly because they are the same? you'll need to add some more information about your issue.. – Jake Long Commented Aug 23, 2012 at 18:59
Add a ment  | 

3 Answers 3

Reset to default 11

You might have forgotten the doctype <!DOCTYPE html> at the beginning of the page.

That's because your document fills the viewport (the zone accessible for displaying the document).

From the documentation :

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

If you document is long enough to take more than one page, the second value may be greater than the first (at least if the document is inside an iframe, look at this demo). There can be other cases generating differences but I have none in mind now.

I had a similar issues that I was able to fix. I found that

$(window).height(); 

Was returning the entire height of the page in FF. Eventually I realized that I was outputting a debug phrase 'test' before any of the html of the page. IE before the Doc type.

Once this 'test' text was removed the view port height was return as expected.

发布评论

评论列表(0)

  1. 暂无评论