As it was shown in Example 3, a simple box surrounding text or images, with custom border and background colors, can be made up of two nested single-cell tables. The cellpadding and bgcolor attributes of the outer table specify box border's width and color respectively. Those attributes for the inner one define the gap between the border and contents, and the background color of the box. However, since the box is a really useful template, I'd like to write separate functions for it. All the functions below reside in templates.js.
function jstBoxOpen(boxWidth, brdWidth, brdCol, bgCol, innerGap, innerAlign, boxHeader, hdCss) { var s = '<table border="0"' + wAttr(boxWidth); s += padAttr(brdWidth) + ' cellspacing="0"><tr>'; s += '<td' + bcAttr(brdCol) + '>'; s += '<table border="0" width="100%"' + padAttr(innerGap) + ' cellspacing="0">'; if (boxHeader != '') { s += '<tr><th' + cssAttr(hdCss) + bcAttr(brdCol) + '>'; s += boxHeader + '</th></tr>'; } s += '<td' + bcAttr(bgCol) + makeAttr('align', innerAlign) + '>'; return s; } function jstBoxClose() { return '</td></tr></table></td></tr></table>'; }The jstBoxOpen() function appears to be really heavy due to the enormous number of parameters: the total box width (percentage values or the number of pixels, if empty string - slim box comes up), border width, border color, background color, inner gap (the cellpadding attribute of the inner table), a CSS class associated with th element of the inner table (hdCss should not conflict with brdCol), the content alignement ('left' by default), and the header. If the header exists (not empty), its properties are defined by CSS class for th element specified through boxCss. The background color of the header is the same as the border color and should not be overridden in CSS (no idea if it works). No style for the content is defined since normally the box will contain other HTML elements rather than pure text.
Note: If you don't specify the box background color, you get the border color instead of the parent element's background. If you really need a transparent box, see "framebox" templates on the next page.
In the real world, to avoid a big mess, you may want to define your own custom boxes on the base of the generic one.
Here's a simple box that can be used as a container for whatever web stuff.
function boxOpen(brdW, brdC, innerW, bgC) { var totalW = (innerW != '') ? (parseInt(innerW) + (brdW * 2)) : ''; return jstBoxOpen(totalW, brdW, brdC, bgC, 0, 'center', '', ''); } function boxClose() { return jstBoxClose(); }The brdW and brdC parameters are border's width and color respectively. For some particular reason I replaced the total box width with the inner width (innerW), which must be specified (if not empty) in pixels only (because the total width is calculated from this one). Since the width parameter might be of either integer or string type, the parseInt function is used to to make sure that string concatenation doesn't happen instead of math addition. Originally I designed this box as global or intermediate level web stuff container, that normally have more tables inside. Therefore the inner cellpadding here is zero, and the align attribute is 'center'. The closing boxClose() function returns the generic one jstBoxClose() 'as is' with no alteration. This is done for the name matching sake only.
This template is shown in Example 4 below.
The next JST pair derived from the generic box template I would like to show here is a custom box with header. Actually this template is similar to that used for the presentation of code examples on these pages.
function boxHdOpen(brdW, brdC, innerW, bgC, aHdr, hdrStyle) { var totalW = (innerW != '') ? (parseInt(innerW) + (brdW * 2)) : ''; return jstBoxOpen(totalW, brdW, brdC, bgC, 0, 'center', aHdr, hdrStyle); } function boxHdClose() { return jstBoxClose(); }This template differs from the previous one by two extra parameters: header text aHdr and header style hdrStyle. The latter should be defined elsewhere before being used. The closing function remains the same.
The following example includes 3 files: templates.js where the templates belong, external style sheet ex04.css, and HTML page where the show happens.
This is the style sheet:
body {background-color: #d4d0c8;} h1 {font-family: Verdana, Arial, sans-serif; font-size: 16pt; color: #0066cc;} h2 {font-family: Arial, sans-serif; font-size: 12pt; color: #000000;} td.box1 {font-family: Verdana, Arial, sans-serif; font-size: 8pt; color: #0000cc;} td.box2 {font-family: 'Courier New', monospace; font-size: 8pt; color: #cc0000;} th.hdr {font-family: Arial, sans-serif; font-size: 12pt; color: #ffffcc;}And here is the HTML front page:
<html><head><title>Example 4</title> <link rel=stylesheet type="text/css" href="ex04.css"> <script language="JavaScript" src="templates.js"></script> </head> <body> <h1>Example 4: Box JST</h1> <h2>Basic box</h2> <script language="JavaScript"> <!-- document.write(boxOpen(1, '#000000', 300, '#ffff00')); document.write(oneCellOpen('100%', 8, '', 'box1', '')); //--> </script> The box properties: border width is 1 px, inner width is 300 px, border color is black, background color is yellow. Inner invisible single-cell table has padding of 8. Content style 'td.box1' is defined in 'ex04.css'. <script language="JavaScript"> <!-- document.write(oneCellClose() + boxClose()); //--> </script> <h2>Box with header</h2> <script language="JavaScript"> <!-- var myHdr = 'A Good Header'; var myHdrStyle = 'hdr'; document.write(boxHdOpen(2, '#006699', 400, '#ccffff', myHdr, myHdrStyle)); document.write(oneCellOpen('100%', 12, '', 'box2', '')); //--> </script> The box properties: border width is 2 px, inner width is 400 px, border color is '#006699', background color is '#ccffff'. Header style 'th.hdr' and content style 'td.box2'; are defined in 'ex04.css'. Inner table's cellpadding is 12. <script language="JavaScript"> <!-- document.write(oneCellClose() + boxHdClose()); //--> </script> </body></html> show