Forum Moderators: open

Message Too Old, No Replies

How the heck do I get the height of the page?

The obvious methods aren't working....

         

MichaelBluejay

10:20 am on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I want to get the approximate height of the page in pixels, which for my test page should be about 950. I found the various properties that look like they should work but they don't. Here's my code and the results it produces:

<HTML><HEAD>
<SCRIPT LANGUAGE=javascript>
function showHeight() {
document.write('<P>document.body.scrollHeight=' +document.body.scrollHeight);
document.write('<BR>document.body.clientHeight=' +document.body.clientHeight);
document.write('<BR>document.body.offsetHeight=' +document.body.offsetHeight);
document.write('<BR>document.documentElement.scrollHeight=' +document.documentElement.scrollHeight);
document.write('<BR>document.documentElement.clientHeight=' +document.documentElement.clientHeight);
document.write('<BR>document.documentElement.offsetHeight=' +document.documentElement.offsetHeight);}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<SCRIPT language=javascript>showHeight()</script>
<TABLE HEIGHT=950><TR></TD></TR></TABLE>
</BODY></HTML>

----------
IE6:

document.body.scrollHeight=15
document.body.clientHeight=413
document.body.offsetHeight=417
document.documentElement.scrollHeight=417
document.documentElement.clientHeight=0
document.documentElement.offsetHeight=417

Safari/Mac:

document.body.scrollHeight=652
document.body.clientHeight=0
document.body.offsetHeight=16
document.documentElement.scrollHeight=54
document.documentElement.clientHeight=70
document.documentElement.offsetHeight=86

IE5/Mac:

document.body.scrollHeight=630
document.body.clientHeight=630
document.body.offsetHeight=630
document.documentElement.scrollHeight=undefined
document.documentElement.clientHeight=undefined
document.documentElement.offsetHeight=100

----------
The funny thing is, not only does each browser return a different set of numbers for the different variables, not even ONE of them is close to 950!

Thanks for your help, -MBJ-

wdsriram

11:42 am on May 28, 2004 (gmt 0)



very difficult to do that try taking a look at msn site they have done something on the same lines.

<snip>

[edited by: korkus2000 at 11:55 am (utc) on May 28, 2004]
[edit reason] no sigs please [/edit]

Alternative Future

12:17 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure on how this fares on the Mac but I tested it in both IE, NS & Firebird with no problems and all returning the height.

<html>
<head>
<script language="Javascript">
function getDocHeight(){
docHeight = document.getElementById("marker").offsetTop;
alert(docHeight);
}
</script>
</head>
<body onload="getDocHeight()" topmargin=0>
<table height=553>
<tr>
<td>&nbsp;</td>
</tr>
</table>
<div id="marker"/>
</body>
</html>

Would be interested in the results.

-George

MichaelBluejay

6:48 am on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Alternative Future, that's certainly a clever way to do it, and I was well on my way to getting this to work when I ran into another problem....

What I'm trying to do is to get a set of 5 items to keep repeating on the page as long as there's room. For example, if there's room for eight more items then I want to print #1, #2, #3, #4, #5, then start over with #1, #2, and #3. Should be a piece of cake, right? The problem is that the first five items print just fine, but they won't repeat. What's weirder is that my debugging code shows that my script is executing all the code with the proper values properly.

Here's my code, with the debugging code commented out. I aplogize for the lack of formatting, I've never been able to get [ pre ] and [ code ] to work on WebmasterWorld to preserve spaces.

------------------------------
<HTML><HEAD>
<script language=javascript>

function fillerUp() {

pageLength = document.getElementById("marker").offsetTop;

stockHeight = 755;
itemHeights = new Array(30,125,125,125,225);
itemContent = new Array('a','b','c','d','e');

for (counter=0, item=0, usedHeight=stockHeight, theString="";
usedHeight <=pageLength; counter++, item++) {

if (usedHeight + itemHeights[counter] <=pageLength)
{ document.getElementById('filler'+counter).innerHTML = itemContent[item]; }
//theString += "document.getElementById('filler" +counter+ "').innerHTML = " +itemContent[item] + "<P>\n";
usedHeight += itemHeights[item];
if (item==itemHeights.length-1) { item=-1; }
}
//document.write(theString);
}
</script>
</HEAD>

<BODY onload="fillerUp()">

<TABLE HEIGHT=2000><TR><TD valign=top>
<div id=filler0></div>
<div id=filler1></div>
<div id=filler2></div>
<div id=filler3></div>
<div id=filler4></div>
<div id=filler5></div>
<div id=filler6></div>
<div id=filler7></div>
<div id=filler8></div>
<div id=filler9></div>
<div id=filler10></div>
<div id=filler11></div>
<div id=filler12></div>
<div id=filler13></div>
<div id=filler14></div>
<div id=filler15></div>
<div id=filler16></div>
</TD></TR></TABLE>

<div id=marker></BODY</HTML>
[/pre]
[/code]

Rambo Tribble

1:34 pm on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps a more direct way to measure the height of the document would be the following. I have tested it on IE 6, Moz 1.5, and Opera 7.23.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
html,body{height:100%;}
</style>
</head>
<body id="pgBod" onload="alert(document.getElementById('pgBod').offsetHeight);">

</body>
</html>

Rambo Tribble

4:02 pm on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure it is the source of your problem, but you div at the bottom should be closed, as in:

<div id="marker"></div>

This may be preventing the return of a value for the page height.

MichaelBluejay

6:29 pm on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the suggestions, Rambo. I'm afraid that didn't solve the current problem, though. I can already get the height of the page just fine, thanks to the previous suggestion. The problem now is that the elements I'm trying to write don't repeat after the first set. Any thoughts?

Rambo Tribble

8:02 pm on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm wondering if the assignment of stockHeight to usedHeight at the beginning of your counter isn't making your if() condition false after the first iteration? Shouldn't usedHeight start at 0 and increment as objects are added to the display?

MichaelBluejay

2:10 am on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, I only want my elements to start repeating at about 750 pixels down the page, that's the "stock height" of the page. Also, I think my assignment is correct. I did put the debugging code in there. Uncomment it and run it and you'll see that it keeps running through after it prints the first set.

This is a real stumper, that's for sure.

Rambo Tribble

2:35 am on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you test the value of the page height, what is the number of px that is returned? Remember also, that the value returned is a string, not a number. You'll need to strip it of the px and cast it to an integer before you can use it in a mathematical test.

(Sorry, I don't have time to mess with it directly or really pick it apart, I'm just throwing out ideas.)

MichaelBluejay

9:29 am on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rambo, I appreciate your replies, but they're not really helpful. As I explained, I can get the page height just fine, there is zero problem there. The problem is with the output. When I uncomment my debugging lines this is what I get:

document.getElementById('filler0').innerHTML = a
document.getElementById('filler1').innerHTML = b
document.getElementById('filler2').innerHTML = c
document.getElementById('filler3').innerHTML = d
document.getElementById('filler4').innerHTML = e
document.getElementById('filler5').innerHTML = a
document.getElementById('filler6').innerHTML = b
document.getElementById('filler7').innerHTML = c
document.getElementById('filler8').innerHTML = d
document.getElementById('filler9').innerHTML = e

----------------
Ergo, if that code executes then it should work, but it doesn't. I've checked the math and the math works fine. Again, neither the math nor the page height is the problem. I worry that this thread will get so cluttered that no one will bother with it and I won't get any help. My code is there for anyone who wants to try it.

Thanks, -MBJ-

Alternative Future

10:38 am on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi MichaelBluejay,

I don't have much time to figure out what you are trying to do, but, the problem you are having is that you are trying to loop around the array or items greater than 5 times which results in a NaN (not a number) JavaScript error. The hack i have in place might not assist you any further but it will show where the problem is. I have added another variable named extra, on the first entry to the for statement this value is an empty string and when the counter reaches 5 it resets it back to 0 along with item, it then adds the char 'a' to the extra which is then used to call on the document.getElem...

function fillerUp() {
pageLength = document.getElementById("marker").offsetTop;
extra = '';
stockHeight = 755;
itemHeights = new Array(30,125,125,125,225);
itemContent = new Array('a','b','c','d','e');

for (var counter=0, item=0, usedHeight=stockHeight, theString="";usedHeight <=pageLength; counter++, item++) {
if(counter==5){counter=0;item=0;extra='a';alert(extra);}
alert(usedHeight + itemHeights[counter]);
if (usedHeight + itemHeights[counter] <=pageLength){
alert(usedHeight + itemHeights[counter]);
document.getElementById('filler'+counter+''+extra).innerHTML = itemContent[item];
}
usedHeight += itemHeights[item];
if (item==itemHeights.length-1) {
item=-1;
}
}
}

<div id=filler0></div>
<div id=filler1></div>
<div id=filler2></div>
<div id=filler3></div>
<div id=filler4></div>
<div id=filler5></div>
<div id=filler0a></div>
<div id=filler1a></div>
<div id=filler2a></div>
<div id=filler3a></div>
<div id=filler4a></div>
<div id=filler5a></div>

Give that a run and see what you get and see where it breaks next, as I get time I shall check back and see if I can assist any further...

-George

Rambo Tribble

3:56 pm on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure this addresses all the constraints you may be dealing with, but given the original parameters of function you expressed, the following may offer a viable and simplified approach. Obviously, some modification would be needed to meet a particular case.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#main{height:2000px;}
</style>
<script type="text/javascript">
function fillPg(){
var pg_ht=document.getElementById("main").offsetHeight;
var cont_ht=new Array(200,300);
var cont_str=new Array("alpha","beta");
var ttl_ht=0;
var wrt_str="";
var arr_ln=cont_str.length;
while(ttl_ht<pg_ht){
for(var i=0;i<arr_ln;i++){
wrt_str+="<div>"+cont_str[i]+"</div>";
ttl_ht+=cont_ht[i];
}
}
document.getElementById("main").innerHTML=wrt_str;
}
</script>
</head>
<body onload="fillPg();">
<div id="main">
</main>
</body>
</html>

Alternative Future

4:11 pm on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nicely done Rambo Tribble!

-George

Rambo Tribble

6:13 pm on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you, George. I should point out I made an error in my closing div tag, it should be </div> not </main>.

MichaelBluejay

11:02 pm on May 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Problem solved, I used the wrong variable name in one place.

In this line:

if (usedHeight + itemHeights[counter] <=pageLength)

... I should have said "item" instead of "counter".

The reason I didn't catch it with my debugging code was that I foolishly didn't put the debugging code *inside* the IF condition I was testing.

Thanks for everyone's help, sorry to waste your time when it came down to my using the wrong variable and not debugging it properly.