Click to See Complete Forum and Search --> : Using JS to set image height in NS4.x


game_on
12-24-2002, 08:31 AM
NETSCAPE 4.x

Hi, I am trying to set an image on my page to be the same as the height (not window.height) of the whole html page.

I can access the height of the page (not window) using:
document.height

Great! - or so I thought.



What I need to do now is set the height of an image to be equal to 'document.height'.

This can be done in a number of ways (I hope).

One method is putting the following code in the page:

<script language="JavaScript">
images = new Array
images[1] = "/images/tiny_invisible.gif";
var myimage = images[1]
document.write('<IMG src="' + myimage + '" width=1 height='+document.height+' border="1" alt="">')
</script>

The problem here is that it will only set its height to the height of the page above it, rather than the whole page. - this make sense as the rest of the page hasn't rendered yet, and therefore won't have a height.



What I need is something that is called one the page has initialised. So, as far as I see that leaves only <body onload="function();">

So, I used the function:

<SCRIPT LANGUAGE="JavaScript1.2">
<!--//
function gettheheight() { //v3.0
alert('document.height= '+document.height);
}//-->
</script>

Whoohay, now I have the total page height. All that remains is to set the height of an image on the page to be this height.

Easier said than done.

Initially, I tried the following:


Function:
<SCRIPT LANGUAGE="JavaScript1.2">
<!--//
function gettheheight() { //v3.0
document.myimage.height=document.height;
}//-->
</script>

And in the page:
<img src="image.gif" width="20" height="20" border="1" vspace="0" name="myimage">



Needless to say this didn't work. I have tried, I have read stuff. Now I'm going to the experts.

Is there anything that can be done to get this to work on netscape 4.x?

Any effort is hugely appreciated - been trying for a week.

M@

swon
12-24-2002, 08:45 AM
Use something like this, the function is called at the end of the page:


<html>
<head></head>
<body>
<tr valign="top">
<td>
<script language="JavaScript">
function makeimage(x)
{
document.write('<img src="image.jpg" width="200" height="'+ x +'">');
}
</script>
</td><td><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></td>
</tr>
</table>
</body>
<SCRIPT LANGUAGE="JavaScript1.2">
x=document.height;
alert('document.height= '+ x);

makeimage(x);
</script>
</html>

game_on
12-24-2002, 09:07 AM
That's so nearlly there.

My proplem is that the image is acually the height - some other elements.

It is beeing usind to force the height of a table - so the image actually appear in the middle of the page, rather than at the end.

I am still trying to play with the code to see if I can make it do this....

Not been able to yet.

M@

swon
12-24-2002, 09:17 AM
Put the whole function makeimage(x) where you want to have the image.

game_on
12-24-2002, 09:22 AM
Tried that:

<html>
<head></head>
<body>
<table BORDER="1">
<tr valign="top">
<td>
<script language="JavaScript">
function makeimage(x)
{
document.write('<img src="/softjam/images/tiny_invisible.gif" border="1" width="200" height="'+ x +'">');
}
</script>

</td><td><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></td>
</tr>
</table><BR>
<BR>

</body>
<SCRIPT LANGUAGE="JavaScript1.2">
x=document.height;
alert('document.height= '+ x);
makeimage(x);
</script>
</html>


It still outputs it at the bottom....

M@)

swon
12-24-2002, 09:42 AM
And this one? ( the td width the script within must be on one line)

<html>
<head></head>
<body>
<table BORDER="1">
<tr valign="top">
<td><img src=/softjam/images/tiny_invisible.gif border=1 width="200" height="javascript:function makeimage(x){document.writeIn(x);}">
</td><td><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></td>
</tr>
</table><BR>
<BR>

</body>
<SCRIPT LANGUAGE="JavaScript1.2">
x=document.height;

alert('document.height= '+ x);
makeimage(x);
</script>
</html>

game_on
12-24-2002, 09:57 AM
Hi mate, it gives the javascript error:

- makeimage is not defined

Presumably because makeimage(x) is being called before the function has been loaded...

??

M@)