tags:

views:

41

answers:

1

I am new to JavaScript with this being my first little app. I am working on this Calendar / Date Time Picker program. I seem to have entered a nightmare scenario where the app crashes and I cannot figure out what is doing the crashing. After some primitive sleuthing (ie successive document.writes to pinpoint the line which kills it, I seem to have narrowed in on a very innocent line of code:

month_start = new Date(this.Year, this.Month, 1);

this.Year and this.Month are attributes of the class/parent function. I checked them and they both contain valid values at this point. I placed a document.write before this line and it displays, but immediately following this line the program is not responsive and does not display a document.write.

Is there some kind of JavaScript 101 mistake I am making here?

This line is inside a pretty small function. The whole function looks like this:

function Display_Calendar_Week(x, month_days){
ds = '';
d=0;
//for the first day of the month, start at the appropriate day of the week
if(x==1)
{
    //first date of current month and year

    month_start = new Date(this.Year, this.Month, 1);

    d = month_start.getDay();
}
for(j=0;j<7;j++)
{
    if( (j<d) || (x > month_days))
    {
        ds +=('<div class="Calendar_Day_Square_Empty"></div>');
    }
    else
    {
        if((x==RightNow_Date) && (RightNow_Month==this.Month) && (RightNow_Year==this.Year))
        {

            ds +=('<div class="Calendar_Day_Square_Today" onClick="'+this.InstanceName+'.Select_Date(\''+ this.PageElement +'\', '+ this.Year +', '+ this.Month +', '+ x +');">' + x + '</div>');
        }
        else if((x==this.Selected_Date) && (this.Selected_Month==this.Month) && (this.Selected_Year==this.Year))
        {
            ds +=('<div class="Calendar_Day_Square_Selected" onClick="'+this.InstanceName+'.Select_Date(\''+ this.PageElement +'\', '+ this.Year +', '+ this.Month +', '+ x +');">' + x + '</div>');
        }
        else
            ds +=('<div class="Calendar_Day_Square" onClick="'+this.InstanceName+'.Select_Date(\''+ this.PageElement +'\', '+ this.Year +', '+ this.Month +', '+ x +');">' + x + '</div>');
        x++;
    }
}
return [x, ds];

}

A: 

In your function, this.Year and this.Month are undefined. You never assign them any values, and this is why the new Date(undefined, undefined, 1) stops the execution. The same goes for the this.PageElement, this.Selected_Month, etc - but the script never gets there.

And a few tips: consider using var when declaring variables - this way, they won't be in the global (window) scope; also, string concatenation is way slower than building an array of strings and joining it: ['a', 'b', 'c'].join('');

Alexander Gyoshev
The [x, y, z].join(''); part is optimization, so it should be done at the end [in my opinion, it is ugly, while it is also premature-optimization]
ItzWarty
IMHO it's cleaner than concatenation - considervar html = [];html.push('<div class="');html.push(divCssClass); // etc.- it makes the code much more readable, especially if you wrap it in a chainable stringBuilder.
Alexander Gyoshev