Перед вам скрипт календаря.
<script language="JavaScript">
setCal()
function helper(day, mounth)
{
scrollTope = document.documentElement.scrollTop+125;
document.getElementById("helper").style.top=scrollTope+'px';
document.getElementById("helper").style.visibility='visible';
document.getElementById("helper").innerHTML=day+'-'+mounth;
var req;
if (window.XMLHttpRequest) // normal browser
req = new XMLHttpRequest();
else if (window.ActiveXObject)
{ //IE
try
{
req = new ActiveXObject('Msxml2.XMLHTTP'); // IE разных версий
}
catch (e)
{
}// может создавать
try
{ // объект по разному
req = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e)
{
}
}
url='core/PHP_Script/day.php?day='+day+'&mounth='+mounth;
req.open("GET", url, true);
req.onreadystatechange = function()
{
if (req.readyState == 4) { document.getElementById("helper").innerHTML=req.responseText; }
else if(req.readyState == 1) { document.getElementById("helper").innerHTML='Загрузка...'; }
};
req.send(null); //отправка запроса без параметров, т.к. они установлены в url
}
function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
/* else */ // else not needed when statement is "return"
return false // is not leap year
}
function getDays(month, year) {
// create array to hold number of days in each month
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December
// return number of days in the specified month (parameter)
return ar[month]
}
function getMonthName(month) {
// create array to hold name of each month
var ar = new Array(12)
ar[0] = "Январь"
ar[1] = "Февраль"
ar[2] = "Март"
ar[3] = "Апрель"
ar[4] = "Май"
ar[5] = "Июнь"
ar[6] = "Июль"
ar[7] = "Август"
ar[8] = "Сентябрь"
ar[9] = "Октябрь"
ar[10] = "Ноябрь"
ar[11] = "Декабрь"
// return name of specified month (parameter)
return ar[month]
}
function setCal() {
// standard time attributes
var now = new Date()
var year = <?php echo date('Y'); ?>
var month = now.getMonth()
var monthName = getMonthName(month)
var date = now.getDate()
now = null
// create instance of first day of month, and extract the day on which it occurs
var firstDayInstance = new Date(year, month, 1)
var firstDay = firstDayInstance.getDay()
firstDayInstance = null
// number of days in current month
var days = getDays(month, year)
// call function to draw calendar
drawCal(firstDay + 1, days, date, monthName, year,month)
}
function drawCal(firstDay, lastDate, date, monthName, year, month) {
// constant table settings
var headerHeight = 30 // height of the table's header cell
var border = 0 // 3D height of table's border
var cellspacing = 1 // width of table's border
var headerColor = "black" // color of table's header
var headerSize = "+1" // size of tables header font
var colWidth = 20 // width of columns in table
var dayCellHeight = 25 // height of cells containing days of the week
var dayColor = "darkblue" // color of font representing week days
var cellHeight = 30 // height of cells representing dates in the calendar
var todayColor = "red" // color specifying today's date in the calendar
var timeColor = "purple" // color of font representing current time
var images = <?php
$id_query = mysql_query("SELECT * FROM holidays");
$i=0;
while($id = mysql_fetch_array($id_query))
{
$array[$i]=$id[id];
$i++;
}
$rand = rand(0,$i);
echo ''.$array[$rand];
?>
// create basic table structure
var month2 = month-1
var month3 = month+1
var text = "" // initialize accumulative variable to empty string
text += '<CENTER>'
text += '<div style="padding:9px;border:1px solid black; background-image:url(images/holidays/s' + images + '.jpg)"><TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + ' WIDTH=100%>' // table settings
text += '<TH COLSPAN=7 HEIGHT=' + headerHeight + '><a href="javascript:setCal('+month2+','+year+');"><div style="background:white;border:1px solid black;width:20px;float:left;"> < </div></a><div style="background:white;border:1px solid black;width:108px;float:left;margin-left:2px;margin-right:2px;">' // create table header cell
text += monthName + ' ' + year
text += '</div><a href="javascript:setCal('+month3+','+year+');"><div style="background:white;border:1px solid black;width:20px;float:left;"> > </div></a></TH>' // close header cell
// variables to hold constant settings
var openCol = '<TD WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '><DIV STYLE="border:1px solid black; background:#F90">'
openCol += '<FONT COLOR="' + dayColor + '">'
var closeCol = '</FONT></DIV></TD>'
// create array of abbreviated day names
var weekDay = new Array(7)
weekDay[0] = "Вс"
weekDay[1] = "Пн"
weekDay[2] = "Вт"
weekDay[3] = "Ср"
weekDay[4] = "Чт"
weekDay[5] = "Пт"
weekDay[6] = "Сб"
// create first row of table to set column width and specify week day
text += '<TR ALIGN="center" VALIGN="center">'
for (var dayNum = 0; dayNum < 7; ++dayNum) {
text += openCol + weekDay[dayNum] + closeCol
}
text += '</TR>'
// declaration and initialization of two variables to help with tables
var digit = 1
var curCell = 1
for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
text += '<TR ALIGN="center" VALIGN="top">'
for (var col = 1; col <= 7; ++col) {
if (digit > lastDate)
break
if (curCell < firstDay) {
text += '<TD></TD>'
curCell++
} else {
if (digit == date) { // current cell represent today's date
text += '<TD HEIGHT=' + cellHeight + '>'
text += '<a href="javascript:helper('+digit+','+month+');"><DIV STYLE="border:1px solid black; background:#FC9"><FONT COLOR="' + todayColor + '">'
text += digit
text += '</FONT></DIV></a>'
text += '</TD>'
} else
text += '<TD HEIGHT=' + cellHeight + '><a href="javascript:helper('+digit+','+month+');"><DIV STYLE="border:1px solid #999;background:White">' + digit + '</DIV></a></TD>'
digit++
}
}
text += '</TR>'
}
// close all basic table tags
text += '</TABLE></div>'
text += '</CENTER>'
// print accumulative HTML string
document.write(text)
}
// --></script>
Вся проблема в том что я пытаюсь сделать смену месяцев. передача в функцию отрисовки идет так
text += '<TH COLSPAN=7 HEIGHT=' + headerHeight + '><a href="javascript:setCal('+month2+','+year+');"><div style="background:white;border:1px solid black;width:20px;float:left;"> < </div></a><div style="background:white;border:1px solid black;width:108px;float:left;margin-left:2px;margin-right:2px;">' // create table header cell
text += monthName + ' ' + year
text += '</div><a href="javascript:setCal('+month3+','+year+');"><div style="background:white;border:1px solid black;width:20px;float:left;"> > </div></a></TH>' // close header cell
Пока что все нормально скрипт работает и все выводит. Но стоит мне начать обьявлять переменные в функции как скрипт отказывается работать
// вот сюда выношу переменные month и year
var year = <?php echo date('Y'); ?>
var month = now.getMonth()
setCal(month, year)
function setCal(month, year) {
// standard time attributes
var now = new Date()
// вынес отсюда переменные month и year
var monthName = getMonthName(month)
var date = now.getDate()
now = null ...
Что я собственно не так делаю и как можно исправить?
Работает под браузером Mozilla 4.0.1