Hello, a little background info if you want it, or you can skip down to look at the code.
I was looking for a way to dynamically scale content, and my initial solution (partly taken from various places, partly trial and error), was script #1. And it worked fine, but I felt it was a bit clunky, and would be hard to do some of the things I wanted with it down the line (i.e. nested tables and such).
So I worked up script #2, but it doesn’t scale with the window.onresize, and doesn’t even seem to refresh properly!
If someone could either show me how to get script #2 working, or a way to make script #1 something easier to work with, I would be extremely appreciative! Advance warning, I’m a bit of a Javascript newbie, so if you see anything that makes you cringe or shake your head, please let me know! :rolleyes:
SCRIPT #1
[code=php]
<html>
<head>
<script language=”javascript”>
var maintable = document.createElement(“table”);
var maintable_body = document.createElement(“tbody”);
function buildTable() {
var mybody = document.getElementsByTagName(“body”).item();
var myrow = document.createElement(“TR”);
var mydata = document.createElement(“TD”);
myrow.appendChild(mydata);
maintable_body.appendChild(myrow);
mybody.appendChild(maintable);
maintable.appendChild(maintable_body);
maintable.setAttribute(“border”,”1″);
maintable.setAttribute(“id”,”maintable”);
if (document.body.clientHeight > (document.body.clientWidth*.714))
{
maintable.setAttribute(“width”, (document.body.clientWidth)*.975);
maintable.setAttribute(“height”, (document.body.clientWidth*.714)*.975);
}
else
{
maintable.setAttribute(“width”, (document.body.clientHeight*1.4)*.975);
maintable.setAttribute(“height”, (document.body.clientHeight)*.975);
}
}
function resizeTable() {
if (document.body.clientHeight > (document.body.clientWidth*.714))
{
maintable.setAttribute(“width”, (document.body.clientWidth)*.975);
maintable.setAttribute(“height”, (document.body.clientWidth*.714)*.975);
}
else
{
maintable.setAttribute(“width”, (document.body.clientHeight*1.4)*.975);
maintable.setAttribute(“height”, (document.body.clientHeight)*.975);
}
}
</script>
</head>
<body onload=buildTable() scroll=”no”>
<script>
window.onresize = resizeTable;
</script>
</body>
</html>
SCRIPT #2
[code=php]
<html>
<head>
<script language=”javascript”>
var mt_height;
var mt_width;
// determine height and width for main table
function sizeTable() {
if (document.body.clientHeight > (document.body.clientWidth*.714))
{
mt_width = (document.body.clientWidth)*.975;
mt_height = (document.body.clientWidth*.714)*.975;
}
else
{
mt_width = (document.body.clientHeight*1.4)*.975;
mt_height = (document.body.clientHeight)*.975;
}
}
//build main table
function buildTable() {
sizeTable();
document.write(“<table border=2 height=”+mt_height+” width=”+mt_width+”>”);
document.write(“<tr>”);
document.write(“<td bgcolor=darkgray></td>”);
document.write(“<td bgcolor=lightgrey></td>”);
document.write(“</tr>”);
document.write(“</table>”);
}
</script>
</head>
<body onload=buildTable() scroll=”no”>
<script>
window.onresize = buildTable;
</script>
</body>
</html>
<i>
</i><body onload=buildTable() scroll="no">
<script>
window.onresize = buildTable;
<i>
</i><body onload="buildTable();" scroll="no">
<script type="text/javascript">
window.onresize = buildTable();
[CODE]window.onresize = buildTable;[/CODE]
[CODE]window.onresize = buildTable();[/CODE]
<i>
</i>function buildTable(){
alert ('Boo');
}
window.onresize = buildTable(); // tries to assign an event to a function call, which is impossible
window.onresize = buildTable; // assigns an event to the internal code of the function named "buildTable"
window.onresize = function() { alert("Boo"); } // creates a function code on-the-fly and assigns it to the event
var buildTable = function(){ alert("Boo"); }
/* The above is the same as the following: */
function buildTable(){ alert("Boo"); }
<i>
</i><style type="text/css"><!--
table {
width: 80%;
margin: 0 auto;
border: solid 1px #000 collapse;
}
tr, th, td {
width: 100%;
border: solid 1px #000 collapse;
}
--></style>
<i>
</i><table>
<tr>
<th>Header</th>
<th>Header</th>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
</table>
Now, to your original problem, would you be so kind as to point out why you're doing all of this with JavaScript?[/QUOTE]
[code=php]
<html>
<head>
<script language="javascript">
var maintable = document.createElement("table");
var maintable_body = document.createElement("tbody");
var mt_height;
var mt_width;
function resizeTable() {
if (document.body.clientHeight > (document.body.clientWidth*.714))
{
maintable.setAttribute("width", (document.body.clientWidth)*.975);
maintable.setAttribute("height", (document.body.clientWidth*.714)*.975);
}
else
{
maintable.setAttribute("width", (document.body.clientHeight*1.4)*.975);
maintable.setAttribute("height", (document.body.clientHeight)*.975);
}
}
function buildTable() {
var mybody = document.getElementsByTagName("body").item();
var myrow = document.createElement("TR");
var mydata = document.createElement("TD");
var nestedtable = document.createElement("table");
var nestedtable_body = document.createElement("tbody");
var nestedrow = document.createElement("TR");
var nesteddata = document.createElement("TD");
var mytext = currenttext=document.createTextNode("Text");
mybody.appendChild(maintable);
maintable.setAttribute("border","1");
maintable.setAttribute("id","maintable");
maintable.appendChild(maintable_body);
maintable_body.appendChild(myrow);
myrow.appendChild(mydata);
// This gets rather tedious quite fast, and isn't very efficient
// as far as file size, etc..
mydata.appendChild(nestedtable);
nestedtable.setAttribute("border","5");
nestedtable.setAttribute("id","nt_1");
nestedtable.setAttribute("width","50%");
nestedtable.setAttribute("height","50%");
nestedtable.setAttribute("bgcolor","lightgrey");
nestedtable.appendChild(nestedtable_body);
nestedtable_body.appendChild(nestedrow);
nestedrow.appendChild(nesteddata);
nesteddata.setAttribute("align","center");
nesteddata.appendChild(mytext);
// ad nauseum, and this is just what it takes to get set up.
// I'd REALLY like to get script #2 working if at all possible.
resizeTable();
}
</script>
</head>
<body onload=buildTable() scroll="no">
<script>
window.onresize = resizeTable;
</script>
</body>
</html>
[/code]
[code=php]
<html>
<head>
<script language="javascript">
var maintable = document.createElement("table");
var maintable_body = document.createElement("tbody");
var mt_height;
var mt_width;
function resizeTable() {
if (document.body.clientHeight > (document.body.clientWidth*.714))
{
maintable.setAttribute("width", (document.body.clientWidth)*.975);
maintable.setAttribute("height", (document.body.clientWidth*.714)*.975);
}
else
{
maintable.setAttribute("width", (document.body.clientHeight*1.4)*.975);
maintable.setAttribute("height", (document.body.clientHeight)*.975);
}
}
</script>
<script>
function buildTable(innerTable) {
var mybody = document.getElementsByTagName("body").item();
var myrow = document.createElement("TR");
var mydata = document.createElement("TD");
mybody.appendChild(maintable);
maintable.setAttribute("border","1");
maintable.setAttribute("id","maintable");
maintable.appendChild(maintable_body);
maintable_body.appendChild(myrow);
myrow.appendChild(mydata);
mydata.setAttribute("id","inner_content");
resizeTable();
document.getElementById("inner_content").innerHTML=innerTable;
}
var tableData = "<table height=50% width= 50% border=5 bgcolor=lightgrey> <tr> <td align=center> <b>TEST<b> </td> </tr> </table>"
</script>
</head>
<body onload='buildTable(tableData)'>
<script>
window.onresize = resizeTable;
</script>
</body>
</html>
[/code]
Error: uncaught exception: [Exception... "Not enough arguments [nsIDOMNodeList.item]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: file:///C:/Documents%20and%20Settings/Kevin%20Mackey/Desktop/javascript/growtable_test.html :: buildTable :: line 25" data: no][/QUOTE]
[code=php]<html>
<head>
<script type="text/javascript">
var maintable = document.createElement("table");
var maintable_body = document.createElement("tbody");
var mt_height;
var mt_width;
var w,h;
function defineScale() {
if (self.innerHeight) { // all except Explorer
w = self.innerWidth;
h = self.innerHeight;
} else if (document.documentElement // Explorer 6 Strict Mode
&& document.documentElement.clientHeight) {
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
function resizeTable() {
defineScale();
if (h > (w*.714)) {
maintable.style.width = (w*0.975);
maintable.style.height = (w*0.714)*0.975;
} else {
maintable.style.width = (h*1.4)*0.975;
maintable.style.height = (h*0.975);
}
}
function buildTable(innerTable) {
var mybody = document.body;
var myrow = document.createElement('TR');
var mydata = document.createElement('TD');
mydata.id = 'inner_content';
myrow.appendChild(mydata);
maintable_body.appendChild(myrow);
maintable.style.border = '1px solid purple';
maintable.id = 'maintable';
maintable.appendChild(maintable_body);
mybody.appendChild(maintable);
resizeTable();
document.getElementById('inner_content').innerHTML=innerTable;
}
var tableData =
'<table width='+'100%'+' height='+'100%'+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr height='+'79%'+'>'
+ '<td width='+'71%'+'>'
+ '<table width='+'100%'+' height='+'100%'+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr height='+'8%'+'>'
+ '<td>menu</td>'
+ '</tr>'
+ '<tr height='+'60%'+'>'
+ '<td>'
+ '<table width='+'100%'+' height='+'100%'+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr>'
+ '<td width='+'51%'+'>image</td>'
+ '<td width='+'45%'+'>text</td>'
+ '<td width='+'4%'+'>.</td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '</tr>'
+ '<tr height='+'32%'+'>'
+ '<td>panel</td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '<td width='+'29%'+' rowspan=2>side</td>'
+ '</tr>'
+ '<tr height='+'21%'+'>'
+ '<td>bottom</td>'
+ '</tr>' +
'</table>'
</script>
</head>
<body onload="buildTable(tableData)">
<script>
window.onresize = resizeTable;
</script>
</body>
</html>[/code]
The defineScale function sets the global variables which can be referenced from any other function, so you always have access to the "w" and "h" variables (providing the defineScale function has already run at least once).[/QUOTE]
Put defineScale(); just before your var tableData = '...'; and then try.[/QUOTE]
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">[/CODE]
<i>
</i><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
var maintable = document.createElement("table");
var maintable_body = document.createElement("tbody");
var mt_height;
var mt_width;
var w,h;
function defineScale() {
if (self.innerHeight) { // all except Explorer
w = self.innerWidth;
h = self.innerHeight;
} else if (document.documentElement // Explorer 6 Strict Mode
&& document.documentElement.clientHeight) {
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
function resizeTable() {
defineScale();
if (h > (w*.714)) {
maintable.style.width = (w*0.975);
maintable.style.height = (w*0.714)*0.975;
} else {
maintable.style.width = (h*1.4)*0.975;
maintable.style.height = (h*0.975);
}
}
function buildTable(innerTable) {
var mybody = document.body;
var myrow = document.createElement('TR');
var mydata = document.createElement('TD');
mydata.id = 'inner_content';
myrow.appendChild(mydata);
maintable_body.appendChild(myrow);
maintable.style.border = '1px solid purple';
maintable.id = 'maintable';
maintable.appendChild(maintable_body);
mybody.appendChild(maintable);
resizeTable();
document.getElementById('inner_content').innerHTML =innerTable;
}
defineScale();
var tableData =
'<table width='+w+' height='+h+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr height='+'79%'+'>'
+ '<td width='+'71%'+'>'
+ '<table width='+'100%'+' height='+'100%'+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr height='+'8%'+'>'
+ '<td>menu</td>'
+ '</tr>'
+ '<tr height='+'60%'+'>'
+ '<td>'
+ '<table width='+'100%'+' height='+'100%'+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr>'
+ '<td width='+'51%'+'>image</td>'
+ '<td width='+'45%'+'>text</td>'
+ '<td width='+'4%'+'>.</td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '</tr>'
+ '<tr height='+'32%'+'>'
+ '<td>panel</td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '<td width='+'29%'+' rowspan=2>side</td>'
+ '</tr>'
+ '<tr height='+'21%'+'>'
+ '<td>bottom</td>'
+ '</tr>' +
'</table>'
alert(tableData);
</script>
</head>
<body onload="buildTable(tableData)">
<script>
window.onresize = resizeTable;
</script>
</body>
</html>
<i>
</i>function resizeTable() {
defineScale();
var tableObj = document.getElementById('inner_content');
if (h > (w*.714)) {
tableObj.style.width = (w*0.975);
tableObj.style.height = (w*0.714)*0.975;
} else {
tableObj.style.width = (h*1.4)*0.975;
tableObj.style.height = (h*0.975);
}
}
[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
var maintable = document.createElement("table");
var maintable_body = document.createElement("tbody");
var mt_height;
var mt_width;
var w,h;
function defineScale() {
if (self.innerHeight) { // all except Explorer
w = self.innerWidth;
h = self.innerHeight;
} else if (document.documentElement // Explorer 6 Strict Mode
&& document.documentElement.clientHeight) {
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
function resizeTable() {
defineScale();
var tableObj = document.getElementById('inner_content');
if (h > (w*.714)) {
tableObj.style.width = (w*0.975);
tableObj.style.height = (w*0.714)*0.975;
} else {
tableObj.style.width = (h*1.4)*0.975;
tableObj.style.height = (h*0.975);
}
}
function buildTable(innerTable) {
var mybody = document.body;
var myrow = document.createElement('TR');
var mydata = document.createElement('TD');
mydata.id = 'inner_content';
myrow.appendChild(mydata);
maintable_body.appendChild(myrow);
maintable.style.border = '1px solid purple';
maintable.id = 'maintable';
maintable.appendChild(maintable_body);
mybody.appendChild(maintable);
resizeTable();
document.getElementById('inner_content').innerHTML=innerTable;
}
defineScale();
var tableData =
'<table width='+w+' height='+h+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr height='+(h*.79)+'>'
+ '<td width='+(w*.71)+'>'
+ '<table width='+(w*.71)+' height='+'100%'+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr height='+(h*.07)+'>'
+ '<td>menu</td>'
+ '</tr>'
+ '<tr height='+(h*.47)+'>'
+ '<td>'
+ '<table width='+(w*.71)+' height='+(h*.47)+' border="1" cellspacing="0" cellpadding="0">'
+ '<tr>'
+ '<td width='+(w*.36)+'>image</td>'
+ '<td width='+(w*.32)+'>text</td>'
+ '<td width='+(w*.03)+'>.</td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '</tr>'
+ '<tr height='+(h*.25)+'>'
+ '<td>panel</td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '<td width='+(w*.29)+' rowspan=2>side</td>'
+ '</tr>'
+ '<tr height='+(h*.21)+'>'
+ '<td></td>'
+ '</tr>' +
'</table>'
</script>
</head>
<body onload="buildTable(tableData);">
<script>
window.onresize = resizeTable;
</script>
</body>
</html>[/code]
[code=php]<head>
<script type="text/javascript">
var maintable = document.createElement("table");
var maintable_body = document.createElement("tbody");
var mt_height;
var mt_width;
var w,h;
function defineScale() {
if (self.innerHeight) { // all except Explorer
w = self.innerWidth;
h = self.innerHeight;
} else if (document.documentElement // Explorer 6 Strict Mode
&& document.documentElement.clientHeight) {
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
function resizeTable() {
defineScale();
if (h > (w*.714)) {
maintable.style.width = (w*0.975);
maintable.style.height = (w*0.714)*0.975;
} else {
maintable.style.width = (h*1.4)*0.975;
maintable.style.height = (h*0.975);
}
}
function buildTable(innerTable) {
var mybody = document.body;
var myrow = document.createElement('TR');
var mydata = document.createElement('TD');
mydata.id = 'inner_content';
myrow.appendChild(mydata);
maintable_body.appendChild(myrow);
maintable.style.border = '1px solid purple';
maintable.id = 'maintable';
maintable.appendChild(maintable_body);
mybody.appendChild(maintable);
resizeTable();
document.getElementById('inner_content').innerHTML=innerTable;
}
var tableData =
'<table width="100%" height="100%" width="100%" border="1" cellspacing="0" cellpadding="0">'
+ '<tr height="79%">'
+ '<td width="71%">'
+ '<table width="100%" height="100%" width="100%" border="1" cellspacing="0" cellpadding="0">'
+ '<tr height="8%">'
+ '<td><img src=menu_bg.jpg height="100%" width="100%" border=0></td>'
+ '</tr>'
+ '<tr height="60%">'
+ '<td>'
+ '<table width="100%" height="100%" border="1" cellspacing="0" cellpadding="0">'
+ '<tr>'
+ '<td width="51%"><img src=image_bg.jpg height="100%" width="100%" border=0></td>'
+ '<td width="45%"><img src=iframe_bg.jpg height="100%" width="100%" border=0></td>'
+ '<td width="4%"><img src=rborder_bg.jpg height="100%" width="100%" border=0></td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '</tr>'
+ '<tr height="32%">'
+ '<td><img src=panel_bg.jpg height="100%" width="100%" border=0></td>'
+ '</tr>'
+ '</table>'
+ '</td>'
+ '<td width="29%" rowspan=2><img src=right_bg.jpg height="100%" width="100%" border=0></td>'
+ '</tr>'
+ '<tr height="21%">'
+ '<td><img src=bottom_bg.jpg height="100%" width="100%" border=0></td>'
+ '</tr>' +
'</table>'
</script>
</head>
<body onload="buildTable(tableData)">
<script>
window.onresize = resizeTable;
</script>
</body>
</html>[/code]
0.1.9 — BETA 6.16