I'll start by showing you how to code a basic calendar that shows the current month with the current day highlighted. Then, how to code a webpage that can display several months simultaneously, and finally, a clickable appointments calendar.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Calendars for Your Website

In this article, I'm going to show you how to use Java Script to create calendars for your website. I'll explain the code in excruciating detail so that even if you are not a programmer, you will understand it and feel comfortable using it and modifying it for your own website. In order to follow this example, all you need is a basic idea of how to use html code to create a table. You will also need a basic understanding of how to use the Java Script document.Write statement.

I'll start by showing you how to code a basic calendar that shows the current month with the current day highlighted. Then, I'll show you how to make your calendar a little fancier, how to code a webpage that can display any month of any year or even several months simultaneously, and finally, how to code a clickable calendar that will display appointments you have for the day you clicked on.

The heart of the calendar code is a Java Script loop that draws an html table that represents the calendar. Examine the basic structure of the code shown below below. As you can see, this code creates a for loop within a for loop (nested for loops). The inner loop writes a row of table cells - the days. The outer loop writes lines of "inner loop" - the weeks.


document.writeln("<table border=1>");

for(row = 1; row <= rows; row ++)
{
  document.writeln("<tr>");
  for(col = 1; col < 8; col++)
  {
    document.write("<td>);
    document.write(ndate);
    document.write("</td>");
  }
document.writeln("</tr>");
}

document.writeln("</table>");

As you can see, the code uses three variables; rows, col, and ndate. The line document.write(ndate); writes the date. Replace that line with with the code shown below.


if(nloc < DayofWeek || ndate >= days[month])
{
  document.write(" ");
}
else
{
  ndate++;
  document.write(ndate);
}
nloc++;

The first line of this code introduces three more variables. DayofWeek contains the cell number of the first day of the month. days[month] contains the number of days in the month. I'll explain how those two variables are derived later. nloc keeps a count of the table cells as they are drawn.

In plain Engish, this code reads:

If the table cell number is less than the cell number that the first day of the week starts on, OR if the day number is higher than (or equal to) the number of days in the month, write a blank cell. Otherwise (else) increment (add one) to the date and then write it in the cell.

How does today's date get highlighted? Change the bottom part of the if statement (the else) to that shown below.


else
{
  ndate++;
  if(ndate == day)
  {
    document.write("<font color='#ff0000'>"
      + ndate + "</font>");
  }
  else
  {
    document.write(ndate);
  }
}

Today's actual date is stored in a variable named day. I'll explain how that is derived later. The if statement above checks to see if the day number going into the table cell (ndate) is equal to the day. If it is, it changes the font color to read. Otherwise, the default font color (black) is used.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269