Results 1 to 4 of 4

Thread: window.onLoad question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    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:&nbsp
      <SELECT NAME="selLocale">
       <OPTION VALUE="M1">Mumbai - station A
       <OPTION VALUE="M2">Mumbai - station B
      </SELECT><BR>
      Incident Date:&nbsp;&nbsp</FONT>
      <SELECT NAME="selYear" ONCHANGE="populate(this.form,this.form.selMonth.selectedIndex);">
       <OPTION> </OPTION>
       <OPTION> </OPTION>
      </SELECT>&nbsp
    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.

  2. #2
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    Try commenting this out:

    //window.onLoad = getYears();


    and add this to the BODY tag:

    <body ONLOAD = "getYears();">

    See if that makes a difference.
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    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:&nbsp
      <SELECT NAME="selLocal">
       <OPTION VALUE="M1">Mumbai - station A
       <OPTION VALUE="M2">Mumbai - station B
      </SELECT><BR>
      Incident Date:&nbsp;&nbsp</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>&nbsp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width