My JavaScript seems to crash when I instantiate a new Date Object

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];

}

Answers


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('');


Need Your Help

What is the ideal way to handle window.onpopstate for all browsers?

javascript popstate

For chrome the event fires on page load, but in firefox it fires the rightway, only when you press the backward or forward button. So how to write a script that works fine for all browsers?

Applying strsplit function for multiple files

r lapply strsplit

could somebody spot what I am doing wrong in the step 4 of my codes please (strsplit)?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.