Variable Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Cook

    #1

    Variable Scope

    If I do . . .

    myForm=document .tstForm;
    function initialSetup(){
    myForm.fld01.va lue="Test 01"
    myForm.fld02.va lue="Test 02";
    myForm.fld01.fo cus();
    }

    Then, in the body tag, I do onLoad="initial Setup()",
    the script doesn't work and I get a "myForm has no properties" error

    I know it'll work if I move it within the function, but I figured a
    global variable would retain its value within the function. Why not?
  • steve stevo

    #2
    Re: Variable Scope

    This would be because at this point

    myForm=document .tstForm;

    the form is not created - you will need to initialise this var after page
    loads

    "Jason Cook" <jacook03194@ho tmail.com> wrote in message
    news:d8c76161.0 310210706.1d1b7 908@posting.goo gle.com...[color=blue]
    > If I do . . .
    >
    > myForm=document .tstForm;
    > function initialSetup(){
    > myForm.fld01.va lue="Test 01"
    > myForm.fld02.va lue="Test 02";
    > myForm.fld01.fo cus();
    > }
    >
    > Then, in the body tag, I do onLoad="initial Setup()",
    > the script doesn't work and I get a "myForm has no properties" error
    >
    > I know it'll work if I move it within the function, but I figured a
    > global variable would retain its value within the function. Why not?[/color]


    Comment

    • steve stevo

      #3
      Re: Variable Scope

      Forgot to add -

      you can declare the global var

      var myForm

      outside the function, and then set it calue inside the function

      function init(){
      myForm = document.tstFor m;
      }

      "Jason Cook" <jacook03194@ho tmail.com> wrote in message
      news:d8c76161.0 310210706.1d1b7 908@posting.goo gle.com...[color=blue]
      > If I do . . .
      >
      > myForm=document .tstForm;
      > function initialSetup(){
      > myForm.fld01.va lue="Test 01"
      > myForm.fld02.va lue="Test 02";
      > myForm.fld01.fo cus();
      > }
      >
      > Then, in the body tag, I do onLoad="initial Setup()",
      > the script doesn't work and I get a "myForm has no properties" error
      >
      > I know it'll work if I move it within the function, but I figured a
      > global variable would retain its value within the function. Why not?[/color]


      Comment

      Working...