SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: Reference to undefined property
-
Aug 20, 2003, 18:38 #1
- Join Date
- May 2003
- Posts
- 595
- 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
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; }
Code:if ( frm.selCategory.value == null ) {
Code:if ( frm.selCategory.value = null ) { // and ... if ( frm.selCategory.value ) {
Code:frm.selSubCategory.options[z] = null;
Code:If frm.selSubCategory.options.length ==0
Code:frm.SelGroup.options[frm.SelGroup.options.length] = new Option('Unknown', '0');
Thanks,
Peter
-
Aug 20, 2003, 20:30 #2
- 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; }
-
Aug 20, 2003, 22:58 #3
- Join Date
- May 2003
- Posts
- 595
- 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; } } }
Code:var dropdown = null; dropdown = xDef(frm.selCategory.value); if (dropdown) { subcat = CategoryList[frm.selCategory.value].categoryid; } else { subcat = "0"; }
Warning: function xDef does not always return a value
Source File: http://sitename/rep/new.asp
Line: 310, Column: 6
Source Code:
}
Thanks a lot,
Peter
-
Aug 20, 2003, 23:19 #4
- 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.
-
Aug 21, 2003, 09:12 #5
- 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; }
Code:function xDef() { for(var i = 0; i < arguments.length; ++i) { if (typeof(arguments[i])=="undefined" ) { return false; } } return true; }
-
Aug 21, 2003, 13:14 #6
- 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); }
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Aug 21, 2003, 18:37 #7
- Join Date
- May 2003
- Posts
- 595
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
Originally Posted by Anarchos
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)
-
Aug 21, 2003, 18:41 #8
- Join Date
- May 2003
- Posts
- 595
- 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
-
Aug 21, 2003, 18:52 #9
- Join Date
- May 2003
- Posts
- 595
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Reference to undefined property
Hi Vinny,
Originally Posted by Vincent Puglia
Code:return (retVal);
I like the braces, they keep my pants up.
Thanks,
Peter
-
Aug 21, 2003, 19:51 #10
- 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.
-
Aug 21, 2003, 20:06 #11
- 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...
Code:function xDef() { var ret = true; for (var i = 0; i < arguments.length; ++i) { if (typeof(arguments[i])=="undefined" ) { ret = false; break; } } return ret; }
Formatting is like naming conventions and editors... everyone has their favoritebut 'consistency' is more important than anything.
-
Aug 22, 2003, 16:00 #12
- 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 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.
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Aug 22, 2003, 17:57 #13
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Aug 22, 2003, 18:09 #14
- Join Date
- May 2003
- Posts
- 595
- 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); }
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
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); }
Thanks,
Peter
Bookmarks