|
-
Aug 21st, 2001, 04:36 PM
#1
Thread Starter
Frenzied Member
window.onLoad question
When the onLoad event occurs, do all of the forms objects exist? I think that references to objects in the form are causing the code to terminate. Could it be that the objects don't exist at this time? I get no error message and the page displays without some lists being populated.
I am trying to use some javascript code that I found at The JavascriptSource and I think there is a problem with the code that is executed by the window.onLoad statement. But then, I am very new to java & javascript, so I could be missing something.
Here is the code in question. There are 2 alerts in the first for loop that I used for debugging. I'll refer to them later.
Code:
function getYears()
{
// build the year list - we only need to see last year & the current year
timeC = new Date();
currYear = timeC.getFullYear() - 1;
for (var i = 0; i < document.frmSelect.year.length; i++)
{
alert("i = " + i);
document.frmSelect.year.options[0] = null;
alert("i = " + i);
}
for (var i = 0; i < document.frmSelect.year.length; i++)
{
document.frmSelect.selYear.options[i] = new Option(currYear++);
}
document.frmSelect.selYear.options[1].selected=true;
} // end select_init
window.onLoad = getYears();
// End -->
</SCRIPT>
</HEAD>
<FORM NAME="frmSelect">
<FONT FACE="arial">
<CENTER><B><FONT SIZE="+2">Incident Information</FONT></B></CENTER><HR>
<FONT SIZE="-1">Select from the following to access the desired data. Then press <b>Display Graph</B>.<BR>
Device/station: 
<SELECT NAME="selLocale">
<OPTION VALUE="M1">Mumbai - station A
<OPTION VALUE="M2">Mumbai - station B
</SELECT><BR>
Incident Date:  </FONT>
<SELECT NAME="selYear" ONCHANGE="populate(this.form,this.form.selMonth.selectedIndex);">
<OPTION> </OPTION>
<OPTION> </OPTION>
</SELECT> 
Leaving the option text blank, nothing is populated in the year list and I never see an alert. I also get no error, which I find curious.
If I change the first for statement to
Code:
for (var i = 0; i < 2; i++)
{
alert("i = " + i);
document.frmSelect.year.options[0] = null;
alert("i = " + i);
}
then I see the first alert, but not the second. The reference to document.frmSelect.year.length has been replaced with the hardcoded value 2, making document.frmSelect.year.options[0] the first reference to frmSelect. This is the basis for my belief that the reference to the form/object is terminating code execution.
One other question. What's up with the statement in the above for loop? Why is the subscript set to 0 (zero) and not i? There is another function that updates the day list whenever the onChange event occurs for the year or month. It uses the same algorithm and works, but I have no idea why.
-
Aug 21st, 2001, 07:19 PM
#2
Fanatic Member
Try commenting this out:
//window.onLoad = getYears();
and add this to the BODY tag:
<body ONLOAD = "getYears();">
See if that makes a difference.
-
Aug 22nd, 2001, 08:43 AM
#3
Thread Starter
Frenzied Member
Originally posted by Psyrus
Try commenting this out:
//window.onLoad = getYears();
and add this to the BODY tag:
<body ONLOAD = "getYears();">
See if that makes a difference.
I had already tried that with the same result.
I don't know if it makes a difference, but I'm using frames and this is one of 3 forms on the page.
-
Aug 22nd, 2001, 01:34 PM
#4
Thread Starter
Frenzied Member
I got tired of beating myself over the head with this one so I started calling around the company for some guidence. Someone suggested I put script inside the <select> </select> tags and that worked for me.
I got rid of the window.onLoad statement and the getYears function. The rest of the code that I originally posted now looks like this:
Code:
</HEAD>
<FORM NAME="frmSelect">
<FONT FACE="arial">
<CENTER><B><FONT SIZE="+2">Incident Information</FONT></B></CENTER><HR>
<FONT SIZE="-1">Select from the following to access the desired data. Then press <b>Display Graph</B>.<BR>
Device/station: 
<SELECT NAME="selLocal">
<OPTION VALUE="M1">Mumbai - station A
<OPTION VALUE="M2">Mumbai - station B
</SELECT><BR>
Incident Date:  </FONT>
<SELECT NAME="selYear" ONCHANGE="populate(this.form,this.form.selMonth.selectedIndex);">
<SCRIPT LANGUAGE="JavaScript">
// build the year list - we only need to see last year & the current year
timeC = new Date();
currYear = timeC.getFullYear() - 1;
for (var i = 0; i < 2; i++)
{
document.frmSelect.selYear.options[i] = new Option(currYear++);
}
document.frmSelect.selYear.options[1].selected=true;
</SCRIPT>
</SELECT> 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|