Someone help out a newbie???

    Date: 11/22/05 (Javascript Community)    Keywords: no keywords

    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 = "";
    c1 = "";
    c2 = "";
    c3 = "";
    c4 = "";
    c5 = "";
    c6 = "";
    c7 = "";
    c8 = "";
    c9 = "";
    colon = "";
    
    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("
    "); document.write(year + "-" + month + "-" + day); alert (year); document.write("
    "); 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("
    "); document.write("
    "); 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("
    "); }


    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.

    Source: http://www.livejournal.com/community/javascript/83753.html

« collapsible content || Someone help out a newbie??? »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home