Image

Someone help out a newbie???

Why does this:

date = new Date();
year = date.getFullYear();
document.write(date);
document.write("
");
document.write(year);

work outside of a function, but not inside a function?

The date = new Date(); works in both cases (I can document.write(date)). Outside of the function, I can document.write(year), but not inside the function.



I use this as a separate *.js file, and include it in the header of the document, with a body onLoad of "wc();"...

alert ("start clocks"); // Simple alert message to be sure the script source is available to the site.

c0 = "<img src=\"images/clock/0.gif\" />";
c1 = "<img src=\"images/clock/1.gif\" />";
c2 = "<img src=\"images/clock/2.gif\" />";
c3 = "<img src=\"images/clock/3.gif\" />";
c4 = "<img src=\"images/clock/4.gif\" />";
c5 = "<img src=\"images/clock/5.gif\" />";
c6 = "<img src=\"images/clock/6.gif\" />";
c7 = "<img src=\"images/clock/7.gif\" />";
c8 = "<img src=\"images/clock/8.gif\" />";
c9 = "<img src=\"images/clock/9.gif\" />";
colon = "<img src=\"images/clock/11.gif\" />";

function write_digit (digit) {
	switch (digit) {
		case "0":
			document.write(c0);
			break;
		case "1":
			document.write(c1);
			break;
		case "2":
			document.write(c2);
			break;
		case "3":
			document.write(c3);
			break;
		case "4":
			document.write(c4);
			break;
		case "5":
			document.write(c5);
			break;
		case "6":
			document.write(c6);
			break;
		case "7":
			document.write(c7);
			break;
		case "8":
			document.write(c8);
			break;
		case "9":
			document.write(c9);
			break;
		default:
			document.write(colon);
	}
}

function wc() {

	date = new Date();				// set the date variable.
	year = date.getFullYear();			// get the full year from the date.
	yearString = year.toString();			// convert the year into a string, so that we can split it.
	ysub1 = yearString.substring(0,1);		// get the first digit of the year
	ysub2 = yearString.substring(1,2);		// get the second digit
	ysub3 = yearString.substring(2,3);		// get the third digit
	ysub4 = yearString.substring(3,4);		// get the fourth digit
	month = date.getMonth() + 1;			// get the month.  Since the months array is 0-based, 
							// add 1 to get the commonly recognised month.
	monthString = month.toString();			// convert the month to a string for splitting
	if (monthString.length != 2) {			// if the monthString is not two characters long
		msub1 = "0";				// set the first digit to "0"
		msub2 = monthString.substring(0,1);	// get the second digit.
	} else {					// if the monthString is 2 digits
		msub1 = monthString.substring(0,1);	// get the first digit
		msub2 = monthString.substring(1,2);	// get the second digit
	}
	day = date.getDate();				// get the day portion of the date.
	dayString = day.toString();			// convert it to a string.
	if (dayString.length != 2) {			// if the dayString is not 2 digits
		dsub1 = "0";				// set the first digit
		dsub2 = dayString.substring(0,1);	// get the second digit
	} else {					// if it is 2 digits
		dsub1 = dayString.substring(0,1);	// get the first digit
		dsub2 = dayString.substring(1,2);	// get the second digit
	}

	document.write(date);				// write the date. -- this works.....
	alert (date);					// TESTING	-- this doesn't, and the script just ends here.
	document.write("<br />");
	document.write(year + "-" + month + "-" + day);
	alert (year);
	document.write("<br />");
	document.write(ysub1);
	document.write(ysub2);
	document.write(ysub3);
	document.write(ysub4);
	document.write("-");
	document.write(msub1);
	document.write(msub2);
	document.write("-");
	document.write(dsub1);
	document.write(dsub2);
	document.write("<br />");

	document.write("<div style=\"background-color: #000000;\">");
	write_digit(ysub1);
	write_digit(ysub2);
	write_digit(ysub3);
	write_digit(ysub4);
	write_digit(":");
	write_digit(msub1);
	write_digit(msub2);
	write_digit(":");
	write_digit(dsub1);
	write_digit(dsub2);
	document.write("</div>");
}


This is (if it's not obvious) for a digital clock to appear on the page with the client-side time. Ultimately, I want to get it updating "live". I've been told I need to use setTimeout("wc()", 1000) to get it to update every second.