javascript document height complications - Stack Overflow

Mozilla & IE developers seem to have simultaneously changed the implementation of their height elem

Mozilla & IE developers seem to have simultaneously changed the implementation of their height elements to represent the Opera implementation... which I previously did not have to worry about.

var height = (document.height !== undefined) ? document.height : document.body.offsetHeight;

When performed on a blank document now returns 0 as the height of the document. My implementation requires knowing the true client viewport to dynamically build on. Chrome and Safari are still acting as they used to.

scrollHeight, and clientHeight are acting exactly the same.

To plicate matters document.height and document.body.offsetHeight are now also taking the full height of the document into account instead of only the viewable area as they used to... I tried an old table spacing method and used a 2000px x 1px transparent andthe document height is set to 2000 now.... naturally Chrome and Safari still work as expected and only give the viewable size.

I am very desperate to fix this issue.

Mozilla & IE developers seem to have simultaneously changed the implementation of their height elements to represent the Opera implementation... which I previously did not have to worry about.

var height = (document.height !== undefined) ? document.height : document.body.offsetHeight;

When performed on a blank document now returns 0 as the height of the document. My implementation requires knowing the true client viewport to dynamically build on. Chrome and Safari are still acting as they used to.

scrollHeight, and clientHeight are acting exactly the same.

To plicate matters document.height and document.body.offsetHeight are now also taking the full height of the document into account instead of only the viewable area as they used to... I tried an old table spacing method and used a 2000px x 1px transparent andthe document height is set to 2000 now.... naturally Chrome and Safari still work as expected and only give the viewable size.

I am very desperate to fix this issue.

Share Improve this question edited May 27, 2016 at 17:15 Sebas 21.6k9 gold badges60 silver badges109 bronze badges asked Jan 28, 2010 at 19:57 DavidDavid 131 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The viewport height is not a property of the document, but of the window viewing it. You get the viewport height from window.innerHeight.

The stuff with document is only needed as a fallback for IE, which doesn't provide the window.inner dimensions. IE (technically incorrectly) makes the document.documentElement represent the viewport, so you can get the height from its clientHeight, unless you're in Quirks Mode which (more incorrectly) makes document.body represent the viewport instead. (document.height is totally non-standard; avoid it.)

So in summary, and assuming you need to support Quirks Mode (let's hope you don't):

var height= (
    'innerHeight' in window? window.innerHeight :
    document.patMode!=='BackCompat'? document.documentElement.clientHeight :
    document.body.clientHeight
);

I use this, that I got from James Paldosey's site:

function getDocHeight() {
    //utility function to find dimensions of page
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744837617a4596359.html

相关推荐

  • javascript document height complications - Stack Overflow

    Mozilla & IE developers seem to have simultaneously changed the implementation of their height elem

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信