Forum Moderators: open
Any tricks that would do this?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Auto TD resize to background image</title>
<script type="text/javascript">
function resize(which) {
tempImg = new Image();
which = document.getElementById(which).firstChild;
for(i = 0; i < which.childNodes.length; i++) {
for(j = 0; j < which.childNodes[i].childNodes.length; j++) {
curTD = which.childNodes[i].childNodes[j];
bkg = (curTD.background? curTD.background : curTD.style.backgroundImage).replace(/^url\(¦\)$/g, '');
tempImg.src = bkg;
curTD.style.height = tempImg.height;
curTD.style.width = tempImg.width;
}
}
}
</script>
</head>
<body onload="resize('mytable')">
<table border="1" id="mytable">
<tbody>
<tr>
<td background="http://www.google.com/intl/en/images/logo.gif"> </td>
</tr>
<tr>
<td style="background-image: url(http://www.google.com/intl/en/images/logo.gif)"> </td>
</tr>
</tbody>
</table>
</body>
</html>
Replace broken pipe ¦ with a regular vertical pipe.
[edited by: DrDoc at 12:11 am (utc) on Feb. 5, 2006]
It works perfect as is, but I ran into a glitch when I tried applying it to my page. I'm assigning the <td background via CSS in the <head> section, not in the <td> tag itself. Can you mod your script to account for that? My code basically looks like this:
<head>
<script type="text/javascript">
[your re-size script here]
</script>
<style type="text/css">
.tdbckgrd {
background: url(/pic.gif);
}
</style>
</head>
<body onload="resize('mytable')">
<table id="mytable">
<tr>
<td class="tdbckgrd"> </td>
</tr>
</table>
</body>
bkg = (curTD.background? curTD.background : curTD.style.backgroundImage).replace(/^url\(¦\)$/g, '');
... to:
bkg = (curTD.background? curTD.background : (curTD.style.backgroundImage? curTD.style.backgroundImage : curTD.currentStyle.backgroundImage)).replace(/^url\(¦\)$¦[\"\']/g, '');
Parse error: parse error, unexpected T_CHARACTER, expecting T_STRING or T_VARIABLE or T_NUM_STRING
My code line looks like:
bkg = (curTD.background? curTD.background : (curTD.style.backgroundImage? curTD.style.backgroundImage : curTD.currentStyle.backgroundImage)).replace(/^url\(¦\)$¦[\\"\']/g, '');