Marco Paland

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromMarco Paland
ToMe
SubjectIs there a javascript function to get the document size?
Date18 October 2005 15:47
Hi,

I read your article about

"Obtaining the browser window size"

an the functions you developed and tested with the different browsers
are doing very well! Good job!

But in the arcticle I'm missing the function getDocumentSize() to
obtain the whole size of a document, needed for some scroll effects.

Have you played with / developed such a function to be browser indepented
like your browser size and scrollpos functions?
Seems to be a complicated to determine this information correctly.


Happy to hear from you!
Marco
FromMe
ToMarco Paland
SubjectRe: Is there a javascript function to get the document size?
Date18 October 2005 17:26
arco,

> But in the arcticle I'm missing the function getDocumentSize() to
> obtain the whole size of a document, needed for some scroll effects.

Well, here's the problem. Some (but by no means all) browsers do provide
properties that do this, but they use the same properties as other browsers
use for window size, and they are certainly not standardised. There is no
reliable cross browser way to get the document size.

but ... you can get the size of other elements (assuming their contents are
not positioned). So wrap everything inside a div, and check the offsetHeight
and offsetWidth of the div - this will work in all major browsers, and most
minor ones.

<body onload="alert(document.getElementById('wrapper').offsetHeight)">
<div id="wrapper">
... page content ...
</div>
</body>

But note that with page scrolling scripts, this should not be needed. The
window.scrollBy and scrollTo methods will fail to work if the page is
already scrolled to the bottom, so you can:
1. check how far the page is scrolled
2. try to scroll it
3. check how far it has scrolled now
4. if the number you got in 1 is the same as in 3, then you have reached the
bottom, so you can stop the script or reset.
That is the technique that most page scrolling scripts use.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.