Cominciamo con le semplificazioni. Lo script che hai postato tu si può notevolmente semplificare così:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Esempio</title>
<script type="text/javascript">
function appendCalendar () {
var
oTD, oTH = document.createElement("tr"), oTBody = document.createElement("tbody"),
oTHead = document.createElement("thead"), oTable = document.createElement("table"),
nIter = 0, oMonthStart = new Date(), aMonthsLen = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
aDaysNames = ["Lun", "Mar", "Mer", "Gio", "Ven", "Sab", "Dom"];
for (nIter; nIter < 7; nIter++) {
oTD = document.createElement("th");
oTD.innerHTML = aDaysNames[nIter];
oTH.appendChild(oTD);
}
oTHead.appendChild(oTH);
oMonthStart.setDate(1);
nIter = 0;
for (var nEndBlanks = (oMonthStart.getDay() + 6) % 7, nEnd = aMonthsLen[oMonthStart.getMonth()] + nEndBlanks, nTotal = nEnd + ((7 - nEnd % 7) % 7); nIter < nTotal; nIter++) {
if (nIter % 7 === 0) {
oTH = document.createElement("tr");
oTBody.appendChild(oTH);
}
oTD = document.createElement("td");
oTD.innerHTML = nIter < nEndBlanks || nIter + 1 > nEnd ? "" : nIter - nEndBlanks + 1;
oTH.appendChild(oTD);
}
oTable.appendChild(oTHead);
oTable.appendChild(oTBody);
document.getElementById("calendar").appendChild(oTable);
}
</script>
</head>
<body onload="appendCalendar();">
<div id="calendar"></div>
</body>
</html>
Detto questo, per ottenere quello che vuoi potresti fare così:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Esempio</title>
<script type="text/javascript">
function appendCalendar () {
var
oTD, oTH = document.createElement("tr"), oTBody = document.createElement("tbody"),
oTHead = document.createElement("thead"), oTable = document.createElement("table"),
nIter = 0, oMonthStart = new Date(), aMonthsLen = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
aDaysNames = ["Lun", "Mar", "Mer", "Gio", "Ven", "Sab", "Dom"];
for (nIter; nIter < 7; nIter++) {
oTD = document.createElement("th");
oTD.innerHTML = aDaysNames[nIter];
oTH.appendChild(oTD);
}
oTHead.appendChild(oTH);
oMonthStart.setDate(1);
nIter = 0;
for (var nDay, nEndBlanks = (oMonthStart.getDay() + 6) % 7, nEnd = aMonthsLen[oMonthStart.getMonth()] + nEndBlanks, nTotal = nEnd + ((7 - nEnd % 7) % 7); nIter < nTotal; nIter++) {
if (nIter % 7 === 0) {
oTH = document.createElement("tr");
oTBody.appendChild(oTH);
}
oTD = document.createElement("td");
nDay = nIter - nEndBlanks + 1;
oTD.innerHTML = nIter < nEndBlanks || nIter + 1 > nEnd ?
""
: "<a href=\"data.php?data=" + oMonthStart.getFullYear() + oMonthStart.getMonth() + (nDay / 100).toFixed(2).substr(-2) + "\">" + nDay + "<\/a>";
oTH.appendChild(oTD);
}
oTable.appendChild(oTHead);
oTable.appendChild(oTBody);
document.getElementById("calendar").appendChild(oTable);
}
</script>
</head>
<body onload="appendCalendar();">
<div id="calendar"></div>
</body>
</html>