SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reference to undefined property

    Hi,

    I'm getting the following messages from the (Mozilla) Javascript console.

    Warning: reference to undefined property CategoryList[frm.selCategory.value]
    Source File: http://sitename/rep/new.asp
    Line: 209

    Error: CategoryList[frm.selCategory.value] has no properties
    Source File: http://sitename/rep/new.asp
    Line: 209
    Line 209 (shown in red)is in the following code:

    Code:
    function getSubCategories(categoryid)
      {
      var subcat = null;
      if ( frm.selCategory.value == null ) {
         subcat = "0";
         }
      else {
         subcat = CategoryList[frm.selCategory.value].categoryid;  // Line 209
         }
      var current = (frm.selSubCategory.value) ? SubCategoryList[frm.selSubCategory.value].subcatname : '';
    	    
      for(z = frm.selSubCategory.length - 1; z >= 0; z--)
         {
         frm.selSubCategory.options[z] = null;
         }
      for(z = 0; z < SubCategoryList.length; z++)
         {
         var prod = SubCategoryList[z];
         if(prod.categoryid == subcat)
              { 
              frm.selSubCategory.options[frm.selSubCategory.options.length] = new Option(prod.subcatname, z, (prod.subcatname==current), (prod.subcatname==current));
              }
         }
      
      frm.category.value = CategoryList[frm.selCategory.value].categoryid;
      }
    frm.selCategory.value is showing as null, when I do a 'Page Info' , and I would have thought that by doing:

    Code:
    if ( frm.selCategory.value == null ) {
    to be suffient for a test. I have also tried:

    Code:
    if ( frm.selCategory.value = null ) {
    // and ...
    if ( frm.selCategory.value ) {
    but still no luck. I _think_ the problem lies where the dropdown is "emptied":

    Code:
    frm.selSubCategory.options[z] = null;
    which of course is followed by code to then "fill" the dropdown (there are 3 levels of dropdowns, a hiearchy). In some circumstances, the top level selection may not find any lower level values, and so, the subcategory dropdown list will remain empty (null ?),. How do I check if the list is null, is it:

    Code:
    If frm.selSubCategory.options.length ==0
    One way or another the frm.selCategory.value is an "undefined property", and then later an error stating "has no properties". If I can check whether the property is undefined (or exists ??), then just one line of code:

    Code:
    frm.SelGroup.options[frm.SelGroup.options.length] = new Option('Unknown', '0');
    with the default value for unknown/unused will suffice. I just don't know how to check for the property. (I guess it is part of an object ??).

    Thanks,

    Peter

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's something I use. It returns true if all it's arguments are defined.
    Code:
    function xDef() {
      for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=="undefined") return false;}
      return true;
    }

  3. #3
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    Thanks a lot, your code did the trick.

    Because (being fairly new to JS) I needed to understand the code, I reformatted it to:

    Code:
    function xDef()
       {
         for(var i=0; i<arguments.length; ++i)
         {  
           if(typeof(arguments[i])=="undefined" ) {
              return false;
           }
           else {
              return true;
           }
         }
       }
    and call the function like this:

    Code:
    var dropdown = null;
    dropdown = xDef(frm.selCategory.value);
    
    if (dropdown) {
       subcat = CategoryList[frm.selCategory.value].categoryid;
       }
    else {
       subcat = "0";
       }
    It's working okay, but I noticed in Javascript Console a warning message:

    Warning: function xDef does not always return a value
    Source File: http://sitename/rep/new.asp
    Line: 310, Column: 6
    Source Code:
    }
    which is the last line of the function 'xDef'

    Thanks a lot,

    Peter

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You reformatted it incorrectly. Look at where the braces are in the original version.
    ck :: bringing chris to the masses.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by Anarchos
    You reformatted it incorrectly. Look at where the braces are in the original version.
    Yes, I didn't realise the return true; was outside the FOR loop. I'm much more used to application programming where there is a definite 'end' like:

    Code:
    Do while condition
    ... do some stuff
    End do
    
    For (iteration constraints)
    ... do some stuff
    End  (or End for)
    
    If (condition) then
     ... do some stuff
    else
      ... do other stuff
    End  (or EndIf or End If)
    Peter

  6. #6
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice work, jehoshua.

    But, your function won't work if it is passed more than one argument.

    Here are a some different formatting styles:

    Code:
    function xDef() {
      for(var i = 0; i < arguments.length; ++i) {
        if (typeof(arguments[i])=="undefined" ) {
          return false;
        }  
      }
      return true;
    }
    Code:
    function xDef()
    {
      for(var i = 0; i < arguments.length; ++i) {
        if (typeof(arguments[i])=="undefined" ) {
          return false;
        }  
      }
      return true;
    }
    Code:
    function xDef()
    {
      for(var i = 0; i < arguments.length; ++i)
      {
        if (typeof(arguments[i])=="undefined" )
        {
          return false;
        }  
      }
      return true;
    }
    This last one I consider an 'old' style. I think it is confusing. Nevertheless, I am currently working on some 'legacy' C code which uses this style.
    Code:
    function xDef()
      {
      for(var i = 0; i < arguments.length; ++i)
        {
        if (typeof(arguments[i])=="undefined" )
          {
          return false;
          }  
        }
      return true;
      }

  7. #7
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    Thanks for your examples. The third one you posted was how the code eventually worked out, sio I guess that makes me 'old'.

    Peter

  8. #8
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tend to prefer having only 1 exit point and as few braces as possible:

    Code:
    function xDef()
    {
    var retVal = true;
    for(var i = 0; i < arguments.length; ++i)
    if (typeof(arguments[i])=="undefined" ) 
    retVal = false;
    return (retVal); 
    }


    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  9. #9
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reference to undefined property

    Hi Vinny,

    Quote Originally Posted by Vincent Puglia
    I tend to prefer having only 1 exit point and as few braces as possible:
    It was hard to distinguish, though (for me) if the:

    Code:
    return (retVal);
    was inside or outside the FOR loop, and especially with no indenting and no braces, I found I had to look at it for a lot longer to understand what it was doing. Finally it clicked though, true will always be returned, unless the IF condition is met.

    I like the braces, they keep my pants up.

    Thanks,

    Peter

  10. #10
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like the braces, they keep my pants up.

  11. #11
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tend to prefer having only 1 exit point...
    It is indeed better design. I might do it like this then:
    Code:
    function xDef() {
      var ret = true;
      for (var i = 0; i < arguments.length; ++i) {
        if (typeof(arguments[i])=="undefined" ) {
          ret = false;
          break;
        }  
      }
      return ret;
    }
    This is the way I usually format my javascript. For C/C++ it depends on the history of the project, and if there were coding/formatting conventions used by the previous developer/team.

    Formatting is like naming conventions and editors... everyone has their favorite but 'consistency' is more important than anything.

  12. #12
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    was inside or outside the FOR loop, and especially with no indenting and no braces
    Re the lack of indentation: I plead innocence!! When I wrote the code, it was indented. I evidently had some problems with the forum's quick reply; it even stuck my name within the 'code' elements.

    Re the lack of braces: I learned javascript via C and a few other languages. Result: I always look for the semi-colon ";" I tend to use braces only when there is a chance for confusion (multiple embedded if/elses)

    I like the braces, they keep my pants up.
    Gee, and I thought they were for teeth

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  13. #13
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reference to undefined property

    Hi,

    There was one final (little) bug that had me stumped for a while, so thought to share it. The function:

    Code:
    function xDef()
      {
      var retval = true;
      for(var i=0; i<arguments.length; ++i)
      {  
         if(typeof(arguments[i])=="undefined" )
         {
         retval = false;
         break;
         }
      }
      return (retval);
      }
    wouldn't work when a 'option' (dropdown) was empty. With JS debugger and stepping through the code, the properties of two dropdowns were:

    selCategory
    length 6
    selectedIndex 3
    value "7"
    options.length 6
    options.selectedIndex 3

    selSubCategory
    length 0
    selectedIndex -1
    value ""
    options.length 0
    options.selectedIndex -1
    The dropdown selCategory had values in it, and the dropdown selsubCategory did not (empty). Therefore, the FOR loop in function XDEF wouldn't get executed, because the length was zero for selSubCategory.

    No doubt if I just change it to:

    Code:
    function xDef()
      {
      var retval = true;
      if (arguments.length == 0)
      {   
         retval = false;
      }
      for(var i=0; i<arguments.length; ++i)
      {  
         if(typeof(arguments[i])=="undefined" )
         {
         retval = false;
         break;
         }
      }
      return (retval);
      }
    ... that should fix it.

    Thanks,

    Peter

  14. #14
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

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