Calendar
This script displays a perpetual javascript calendar, you can include ajax calls to update the days links
Examples of use
The code
- /*
- AJAX Calendar
- By Rene Lopez Caballero <http://www.mywebexperiences.com/javascript/calendar/>
- Copyright (c) 2008 Rene Lopez Caballero
- Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
- */
- function RLCalendar(id, month, year) { this.init(id, month, year); }
- RLCalendar.prototype = {
- init: function(id, month, year) {
- this.monthsTitles = [‘January’,‘February’,‘March’,‘April’,‘May’,‘June’,‘July’,‘August’,‘September’,‘October’,‘November’,‘December’];
- this.daysTitles = [‘S’,‘M’,‘T’,‘W’,‘T’,‘F’,‘S’];
- this.dayURL = ”,
- this.monthURL = ”,
- this.selectedDays = [];
- this.id = id;
- var currentDate = new Date();
- this.selectedMonth = this.currentMonth = currentDate.getMonth();
- this.selectedYear = this.currentYear = currentDate.getFullYear();
- this.selectedDay = this.currentDay = currentDate.getDate();
- this.setMonth(month);
- this.setYear(year);
- window.rlc_calendars[this.id]=this;
- },
- getHTML: function(id, month, year) {
- var i, htmlCal, previousMonth, nextMonth, previousYear, nextYear, htmlDia;
- var ap = new Date( this.calendarYear, this.calendarMonth, 1 );
- previousMonth = (this.calendarMonth==0) ? 11 : this.calendarMonth – 1;
- previousYear = (this.calendarMonth==0) ? this.calendarYear-1 : this.calendarYear;
- nextMonth = (this.calendarMonth==11) ? 0 : this.calendarMonth + 1;
- nextYear = (this.calendarMonth==11) ? this.calendarYear + 1 : this.calendarYear;
- var htmlCal = "<table id=’" + this.id + "’>";
- htmlCal += "<caption>"
- htmlCal += "<a href=\"javascript:window.rlc_calendars['"+this.id+"'].update("+(previousMonth+1)+","+previousYear+")\">< </a>";
- htmlCal += this.monthsTitles[this.calendarMonth] + " " + this.calendarYear;
- htmlCal += "<a href=\"javascript:window.rlc_calendars['"+this.id+"'].update("+(nextMonth+1)+","+nextYear+")\"> ></a>";
- htmlCal += "</caption><thead><tr>";
- for( i=0;i<7;i++)
- htmlCal+="<th>"+this.daysTitles[i]+"</th>";
- htmlCal+="</tr></thead>";
- htmlCal+="<tbody>";
- if( ap.getDay() != 0 )
- htmlCal+="<tr>";
- for( i=0;i<ap.getDay();i++)
- htmlCal+="<td> </td>";
- i=1;
- var dayOfTheWeek;
- do{
- dayOfTheWeek = ap.getDay();
- if (ap.getDay() == 0)
- htmlCal+="<tr>";
- if( !this.isEmpty( this.dayURL ) && this.inArray( i ,this.selectedDays ) )
- htmlDia = "<a href=’"+ this.parseDayURL( ap.getDate(), ap.getMonth()+1, ap.getFullYear() ) +"’>" + i + "<a>";
- else
- htmlDia = i;
- if( ( ap.getDate() == this.selectedDay ) && ( this.calendarMonth == this.selectedMonth ) && ( this.calendarYear == this.selectedYear ) )
- htmlCal+="<td id=’today’>"+htmlDia+"</td>";
- else
- htmlCal+="<td>"+htmlDia+"</td>";
- if( dayOfTheWeek == 6)
- htmlCal+="</tr>";
- i++;
- ap.setDate(i);
- } while( ( i< 32 ) && (i == ap.getDate() ) );
- if( i>0 ) {
- for( i=dayOfTheWeek; i<6; i++ )
- htmlCal+="<td> </td>";
- htmlCal+="</tr>";
- }
- htmlCal+="</tbody></table>";
- return htmlCal;
- },
- update: function(month, year){
- this.setMonth(month);
- this.setYear(year);
- if( !this.isEmpty( this.monthURL ) && ( new Date( this.calendarYear, this.calendarMonth, 1 ) <= new Date( this.currentYear, this.currentMonth, 1 ) ) ){
- var cal = this;
- new Ajaxito( this.parseMonthURL( this.calendarMonth, this.calendarYear ),function( resp ){
- cal.setSelectedDays(eval(resp.responseText));
- document.getElementById(cal.id).parentNode.innerHTML = cal.getHTML();
- } );
- } else {
- this.setSelectedDays([]);
- document.getElementById(this.id).parentNode.innerHTML = this.getHTML();
- }
- },
- setMonth: function(month){ this.calendarMonth = (month==undefined) ? this.currentMonth : (month-1); },
- setYear: function(year){ this.calendarYear = year||this.currentYear; },
- setSelectedDay: function(day, month, year) {
- this.selectedDay = day||this.currentDay;
- this.selectedMonth = month||this.currentMonth;
- this.selectedYear = year||this.currentYear;
- },
- setDayURL: function(url){ this.dayURL = url; },
- setDaysTitles: function(titles){ this.daysTitles = titles; },
- setMonthURL: function(url){ this.monthURL = url; },
- setMonthsTitles: function(titles){ this.monthsTitles = titles; },
- setSelectedDays: function(days) { this.selectedDays = days; },
- parseMonthURL: function(month, year){
- return this.monthURL.replace( /%monthnum%/gi, this.zeroise(month+1) ).replace( /%year%/gi, year );
- },
- parseDayURL: function(dia, month, year){
- return this.dayURL.replace( /%day%/gi, this.zeroise(dia) ).replace( /%monthnum%/gi, this.zeroise(month) ).replace( /%year%/gi, year );
- },
- print: function(){ document.write( "<div>" + this.getHTML() + "</div>" ); },
- isEmpty:function(value) { return /^\s*$/.test( value ); },
- inArray:function(value, array) {
- for(var i=0; i<array.length; i++) if(array[i] == value) return true;
- return false;
- },
- zeroise:function(value) { return (value>9)?value:(‘0′+value); }
- }
- function Ajaxito(url, callback) {
- this.onStateChange = function(){
- if (this.transport.readyState == 4 && this.transport.status == 200) {
- if (this.callback) setTimeout(this.bind(function(){this.callback(this.transport);},this), 10);
- this.transport.onreadystatechange = function(){};
- }
- }
- this.bind = function(caller,obj){ return function(){ return caller.apply(obj,arguments); } }
- this.transport = window.ActiveXObject ? new ActiveXObject(‘Microsoft.XMLHTTP’) : ( window.XMLHttpRequest ? new XMLHttpRequest() : false )
- this.callback = callback || null;
- this.transport.open(‘get’, url, true);
- this.transport.onreadystatechange = this.bind(this.onStateChange,this);
- this.transport.send(”);
- }
- window.rlc_calendars = {};