
Originally Posted by
birdman
I've been wondering for years on how to use the "?" and ":" when writing a script.
The construct you're referring to, the conditional operator (?: ), should be used as part of an expression, just like most other operators.
Consider if, the statement equivalent. Its use focuses primarily around code path branches; where certain statements should only occur given a specific condition (or conditions). The conditional operator should be used when a value in an expression should change based on a condition (or conditions).
For example:
Code:
var errors = [];
/* ...
* Some code that may encounter errors to be shown to the user.
* ...
*/
/* If error messages were added to the the errors array, */
if(errors.length) {
/* ...display them in a dialog box. */
alert(errors.length + ' error'
+ ((errors.length > 1) ? 's' : '')
+ ' occurred:\n\n'
+ errors.join('\n'));
}
Here, if there is more than one error message, an 's' is added to form a plural; if only one is present then no suffix is added.
As a result (I believe...), I end up writing a script, the hard way
The repetition can certainly be avoided. In addition, multiple document.write calls can severely slow execution.
Code:
if('function' != typeof Array.prototype.push) {
Array.prototype.push = function(x) {
var i = 0,
j = this.length >>> 0,
n = arguments.length;
while(i < n) {this[j++] = arguments[i++];}
return j;
};
}
function daysInMonth(m, y) {
var d = new Date(y || (new Date()).getFullYear(), m + 1, 0);
return d.getDate();
}
function showEvent(n) {
if('undefined' != typeof events[n]) {alert(events[n]);}
else if(noEvent) {alert(noEvent);}
}
var noEvent = '',
events = {9 : 'Happy birthday Andrea! *hugsssss!',
10 : 'This note feature was immediately put after 30 minutes of completing the first version!\n\nUiTM convo from the 10th to 19th.',
11 : 'Shakespeare\'s Macbeth @ Actors Studio Bangsar tonight - 8:30pm',
14 : 'Double Bill @ KLPac begins!',
19 : 'happy birthday Ellis :D *muaks!'},
days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
now = new Date(),
d = now.getDate(),
m = now.getMonth(),
y = now.getFullYear(),
a = ['<table class="calendar">',
'<caption>' + days[now.getDay()] + ', ' + d + ' ' + months[m] + ' ' + y + '</caption>',
'<tr>'];
for(var i = 1, n = daysInMonth(m, y); i <= n; ++i) {
if(!((i - 1) % 5) && (i - 1)) {a.push('</tr>', '<tr>');}
a.push('<td class="' + ((i == d) ? 'today ' : '') + ('undefined' != typeof events[i] ? 'event' : '') + '" onclick="showEvent(' + i + ')">' + i + '</td>');
}
if((i = n % 5)) {a.push('<td class="empty" colspan="' + (5 - i) + '"></td>');}
a.push('</tr>', '</table>');
document.write(a.join('\n'));
The dates now use one-based indicies, and are added through the events object. The class attribute structure has also been changed. Don't create classes for everything; use the document structure whenever possible. For example, you used 'calTD' for every table cell within the calendar table, when '.calTB td' would have sufficed needing far less markup.
You can see the new classes from looking through the code, but briefly they are:
- calendar - The main class that indicates that a particular table contains a calendar.
- event - This cell class marks that date as one that contains an event.
- today - Obvious, I hope.

- empty - Used on the final cell that appears for months that will create partial rows (February, and 31-day months) so that the border can be styled away (or whatever).
A possible replacement for your current style sheet might be:
Code:
table.calendar {
border-collapse: collapse;
font: 90% sans-serif;
width: 10em;
}
table.calendar caption {
font-weight: bold;
margin-bottom: 0.5ex;
}
table.calendar td {
color: #000000;
background-color: #eeeeee;
border: 1px solid #000000;
cursor: pointer;
text-align: center;
}
table.calendar td.empty {
background-color: transparent;
border-style: none;
}
table.calendar td.event {
color: #1177ff !important;
}
table.calendar td.today {
background-color: #bbbbbb;
color: #ffffff;
font-weight: bold;
}
and uh... the background. i thought it's okay, did u mean it was not readable via i.e or firefox?
Unreadable in Firefox as the background becomes fully visible after about a sixth of the way down the page.
Why does this happen? I'm not entirely sure. When I view the document locally, it doesn't occur, so I assume it's related to the junk injected by GeoCities.
Some general advice, though:
- Dump XHTML. It currently has no value on the Web, unless you content negotiate it to XML-only parsers. As you serve the markup as HTML, it won't even benefit these user agents.
- Stop using 'div soup'. Your markup at the moment is basically div and span elements, with a load of line breaks (br) thrown in. Most of those span elements should be level 2 headings (h2), and most of those div elements should be paragraphs (p). The line breaks can (and should) be replaced by margins (usually provided by default for headings).
- Instead of adding a class attribute to each list item of your lower-roman list, add a single one to the list element itself.
- Mark-up the other two lists as lists, not broken lines.
- Don't use pixel font sizes as IE cannot resize these. Use percentages instead. Moreover, use 100% for body text, 85% of default for legalese, and never go lower; 10px is far too small. Black-on-white at that size is guaranteed to give me, and many others no doubt, a painful headache.
- Unless you are content to use Verdana at its default size, don't use it. If you shrink it down, other fonts (used when Verdana doesn't exist on a system) will probably be completely unreadable.
- Remove the layout table. Float the right-hand column instead.
Finally, to answer a question I just happened to read, a generic font is the type of typeface: serif (TNR), sans-serif (Arial), monospace (Courior New), cursive, and fantasy[1]. As font-family declarations use a fallback mechanism, the last family should be generic in case none of the specified fonts were available. A simple example is:
Code:
font-family: Arial, sans-serif;
As some browsers default to serif fonts, this would ensure that your document was presented with a sans-serif font even if Arial didn't exist (like in Linux).
Hope that helps,
Mike
[1] The latter two don't have well-known examples, but are mainly decorative. Cursive fonts have connected glyphs just like you'd find in handwriting. Fantasy fonts have a rather vague description, but I'd think of them as rare, special typefaces used in logos. For instance, a vampire film might use in its advertising a typeface that features points at the base of stems to convey the association with a vampire's fangs; it's not a functional font but, in this instance, represents an idea.
Bookmarks