Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Could someone tell my why this isn'

Status
Not open for further replies.

belocin

Programmer
Jul 10, 2000
12
US
Could someone tell my why this isn't working?
I checked the faqs to see how to get the size of a table. This function should return the actual width of the cell, or when I switch to 'menutable' - the width of the table.
I keep getting an Object required error
thanks - nicole

*the javascript is on a different page than the table.

<script >
function getCellWidth()
{
var cellwidth;
cellwidth = document.getElementById('menucell').offsetWidth;

return cellwidth;
}




Menu1=new Array(&quot;Saving&quot;,&quot;url&quot;,&quot;&quot;,4,27,getCellWidth());



</script>


<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; id=&quot;menutable&quot;>
<tr height=&quot;35&quot; id=&quot;menurow&quot;>
<td> </td>
<td align=&quot;left&quot; valign=&quot;top&quot; bgcolor=&quot;red&quot; height=&quot;35&quot; width=&quot;20%&quot; id=&quot;menucell&quot;><img src=&quot;/_imgs/_transparent.gif&quot; width=&quot;0&quot; height=&quot;0&quot; alt=&quot;&quot; border=&quot;0&quot;><b><!--<a href=&quot;/saving/index.php3&quot;><font color=&quot;ffffff&quot;>Saving </font></a>--></nobr></b></td>
</tr>
</table>
 
You might try this I added this.cellwidth instead of cellwidth. If this doesn't fix it can you post the js here.

<script >
function getCellWidth()
{
var cellwidth;
this.cellwidth = document.getElementById('menucell').offsetWidth;
return cellwidth;
}
Menu1=new Array(&quot;Saving&quot;,&quot;url&quot;,&quot;&quot;,4,27,getCellWidth());
</script>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; id=&quot;menutable&quot;>
<tr height=&quot;35&quot; id=&quot;menurow&quot;>
<td> </td>
<td align=&quot;left&quot; valign=&quot;top&quot; bgcolor=&quot;red&quot; height=&quot;35&quot; width=&quot;20%&quot; id=&quot;menucell&quot;><img src=&quot;/_imgs/_transparent.gif&quot; width=&quot;0&quot; height=&quot;0&quot; alt=&quot;&quot; border=&quot;0&quot;><b><!--<a href=&quot;/saving/index.php3&quot;><font color=&quot;ffffff&quot;>Saving </font></a>--></nobr></b></td>
</tr>
</table> 10101010101010101010101010100001
If you code it I will crash it.
10101000100101001111000100100010
 
I tried adding this. but it gave me an Object required error again.
I cleaned up the code some, to test on a separate page,Here it is
thanks - nicole

<script language=&quot;JavaScript&quot;>

function getCellWidth()
{
var cellwidth;
this.cellwidth = document.getElementById('menucell').offsetWidth;
return cellwidth;
}

alert(getCellWidth());

</script>
</head>

<body>

<table width=&quot;100%&quot; id=&quot;menutable&quot;>
<tr id=&quot;menurow&quot;>
<td id=&quot;menucell&quot;> 1</td>
<td> 2 </td>
</tr>
</table>
</body>
 

There are two things I changed to get it working.


1. I altered the line:

Code:
this.cellwidth = document.getElementById'menucell').offsetWidth;

to say:

Code:
cellwidth = document.getElementById'menucell').offsetWidth;

2. I prevented the execution of the alert until the table in the was loaded in the document.


This can be done in a end-of-BODY script

- AFTER the table is defined and loaded in the browser,

or in an ONLOAD BODY script

- invoked AFTER the whole document is loaded. (onload)


The following works with 2 alerts.

<html>
<head>

<script language=&quot;JavaScript&quot;>

function getCellWidth()
{
var cellwidth;
cellwidth = document.getElementById('menucell').offsetWidth;
return cellwidth;
}



</script>
</head>

<body onload=&quot; ; alert('from Onload ' + getCellWidth()); &quot; >

<table width=&quot;100%&quot; id=&quot;menutable&quot;>
<tr id=&quot;menurow&quot;>
<td id=&quot;menucell&quot;> 1</td>
<td> 2 </td>
</tr>
</table>


<script language=&quot;JavaScript&quot;>


alert('from End of Body Script ' + document.getElementById('menucell').offsetWidth)

</script>

</body>

 
Put your script in or after the body.
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top