Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Calendar not displaying

Posted on 2014-04-29
1
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

688 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