Solved

Javascript Calendar not displaying

Posted on 2014-04-29
1
207 Views
Last Modified: 2014-04-30
I am having issues getting a calendar to display.  The window.open is working but nothing is displaying.  I don't see what is causing the issue.

<a href="" onclick="displayCalendar(); return false">Select Date</a>

Open in new window


<script type="text/javascript">
/* <![CDATA[ */

function displayCalendar(whichMonth) 
{
	calendarWin = window.open("", "CalendarWindow", "status=no,resizable=yes,width=400,height=220,left=200,top=200");
	calendarWin.focus();
	calendarWin.document.write("<!DOCTYPE html PUBLIC '-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN' ");
	calendarWin.document.write("'http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd'>");
	calendarWin.document.write("<html xmlns='http:\/\/www.w3.org\/1999\/xhtml'>");
	calendarWin.document.write("<head><title>Calendar<\/title>");
	calendarWin.document.write("<meta http-equiv='content-type' content='text\/html;charset=iso-8859-1' \/>");
	calendarWin.document.write("<link rel='stylesheet' href='js_styles.css' type='text\/css' \/><\/head><body>");
	calendarWin.document.write("<table cellspacing='0' border='1' width='100%'>");
	calendarWin.document.write("<colgroup span='7' width='50' \/>");
	calendarWin.document.write("<tr><td colspan='7' align='center'><strong>" + monthArray[month] + " " + dateObject.getFullYear() + "<\/strong><\/td></tr>");
	calendarWin.document.write("<tr align='center'><td>Sun<\/td><td>Mon<\/td><td>Tue<\/td><td>Wed<\/td><td>Thu<\/td><td>Fri<\/td><td>Sat<\/td><\/tr>");
	calendarWin.document.write("<tr align='center'>");
	dateObject.setDate(1);

	var dayOfWeek = dateObject.getDay();
	for (var i=0; i<dayOfWeek; ++i) {
		calendarWin.document.write("<td>&nbsp;<\/td>");
	}

	var daysWithDates = 7 - dayOfWeek;
	var dateCounter = 1;
	for (var i=0; i<daysWithDates; ++i) {
		var curDate = monthArray[month] + " " + dateCounter + ", " + dateObject.getFullYear();
		calendarWin.document.write("<td><a href='' onclick='self.opener.document.forms[0].reservationDate.value=\"" + curDate + "\";self.close()'>" + dateCounter + "<\/a><\/td>");
		++dateCounter;
	}

	var numDays = 0;
	if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
		numDays = 31;
	if (month == 1)
		numDays = 28;
	else if (month == 3 || month == 5 || month == 8 || month == 10)
		numDays = 30;

	for (var rowCounter = 0; rowCounter < 5; ++rowCounter) 
	{
		var weekDayCounter = 0;
		calendarWin.document.write("<tr align='center'>");
		while (weekDayCounter < 7) {
			var curDate = monthArray[month] + " " + dateCounter + ", " + dateObject.getFullYear();
			if (dateCounter <= numDays)
				calendarWin.document.write("<td><a href='' onclick='self.opener.document.forms[0].reservationDate.value=\"" + curDate + "\";self.close()'>" + dateCounter + "<\/a><\/td>");
			else
				calendarWin.document.write("<td>&nbsp;<\/td>");
			++weekDayCounter;
			++dateCounter;
		}
		calendarWin.document.write("<\/tr>");
	}
			  
	calendarWin.document.write("<\/table><\/body><\/html>");
	calendarWin.document.close();
}

/* ]]> */
</script> 

Open in new window

0
Comment
Question by:CipherIS
1 Comment
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40031249
The first three lines I added to the function were undefined in your original code which cause the javascript to fail.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Javascript Calendar Popup</title>
<script type="text/javascript">
/* <![CDATA[ */

function displayCalendar(whichMonth) 
{
	var dateObject = new Date();
	var month = dateObject.getMonth(); 
	var monthArray = new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sept","Oct","Nov","Dec");

	var calendarWin = window.open("", "CalendarWindow", "status=no,resizable=yes,width=400,height=220,left=200,top=200");
	calendarWin.focus();
	calendarWin.document.write("<!DOCTYPE html PUBLIC '-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN' ");
	calendarWin.document.write("'http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd'>");
	calendarWin.document.write("<html xmlns='http:\/\/www.w3.org\/1999\/xhtml'>");
	calendarWin.document.write("<head><title>Calendar<\/title>");
	calendarWin.document.write("<meta http-equiv='content-type' content='text\/html;charset=iso-8859-1' \/>");
	calendarWin.document.write("<link rel='stylesheet' href='js_styles.css' type='text\/css' \/><\/head><body>");
	calendarWin.document.write("<table cellspacing='0' border='1' width='100%'>");
	calendarWin.document.write("<colgroup span='7' width='50' \/>");
	calendarWin.document.write("<tr><td colspan='7' align='center'><strong>" + monthArray[month] + " " + dateObject.getFullYear() + "<\/strong><\/td></tr>");
	calendarWin.document.write("<tr align='center'><td>Sun<\/td><td>Mon<\/td><td>Tue<\/td><td>Wed<\/td><td>Thu<\/td><td>Fri<\/td><td>Sat<\/td><\/tr>");
	calendarWin.document.write("<tr align='center'>");
	dateObject.setDate(1);

	var dayOfWeek = dateObject.getDay();
	for (var i=0; i<dayOfWeek; ++i) {
		calendarWin.document.write("<td>&nbsp;<\/td>");
	}

	var daysWithDates = 7 - dayOfWeek;
	var dateCounter = 1;
	for (var i=0; i<daysWithDates; ++i) {
		var curDate = monthArray[month] + " " + dateCounter + ", " + dateObject.getFullYear();
		calendarWin.document.write("<td><a href='' onclick='self.opener.document.forms[0].reservationDate.value=\"" + curDate + "\";self.close()'>" + dateCounter + "<\/a><\/td>");
		++dateCounter;
	}
	var numDays = 0;
	if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
		numDays = 31;
	if (month == 1)
		numDays = 28;
	else if (month == 3 || month == 5 || month == 8 || month == 10)
		numDays = 30;

	for (var rowCounter = 0; rowCounter < 5; ++rowCounter) 
	{
		var weekDayCounter = 0;
		calendarWin.document.write("<tr align='center'>");
		while (weekDayCounter < 7) {
			var curDate = monthArray[month] + " " + dateCounter + ", " + dateObject.getFullYear();
			if (dateCounter <= numDays)
				calendarWin.document.write("<td><a href='' onclick='self.opener.document.forms[0].reservationDate.value=\"" + curDate + "\";self.close()'>" + dateCounter + "<\/a><\/td>");
			else
				calendarWin.document.write("<td>&nbsp;<\/td>");
			++weekDayCounter;
			++dateCounter;
		}
		calendarWin.document.write("<\/tr>");
	}
			  
	calendarWin.document.write("<\/table><\/body><\/html>");
	calendarWin.document.close();
}

/* ]]> */
</script> 

</head>
<body>
<h1>Javascript Calendar Popup</h1>
<a href="" onclick="displayCalendar(1); return false">Select Date</a>
</body>
</html>

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question