|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Javascript: How to test whether an object is an object?
I am making a tree menu with infinte sub nodes. Now to do this I'm trying to do it the simplest way possible and so I have two function one that expands and one that collapses.
This is the code so far. Code:
function expandMenu(linkId) {
var x = 0;
linkId_new = linkId + "_0";
while(eval(doc + '[linkId_new]' + sty) == [Object]) {
linkId_new = linkId + "_" + x;
linkObj = eval(doc + '[linkId_new]' + sty);
linkObj.display = "block";
x++;
}
}
function collapseMenu(linkId) {
linkObj = eval(doc + "[linkId]" + sty);
linkObj.display = "none";
}
function doIt() {
var expandValue = form.expandId.value
var collapseValue = form.collapseId.value
if(expandValue != "") {
expandMenu(expandValue)
}
if(collapseValue != "") {
collapseMenu(collapseValue)
}
}
now it is very simple i call doIt() and it calls either expand() or collapse(). Now I use this for the actual menus. Code:
<div id="go_0" class="Level1">
<img src="images/frames/left/expands-collapses_plus.gif">
100
<div id="go_0_0" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
110
</div>
<div id="go_0_1" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_plus.gif">
120
<div id="go_0_1_0" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
121
</div>
<div id="go_0_1_1" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
122
</div>
</div>
<div id="go_0_2" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_plus.gif">
130
<div id="go_0_2_0" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_plus.gif">
131
<div id="go_0_2_0_0" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_plus.gif">
139
<div id="go_0_2_0_0_0" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_plus.gif">
138
<div id="go_0_2_0_0_0_0" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
137
</div>
</div>
</div>
</div>
</div>
<div id="go_0_3" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
140
</div>
</div>
<div id="go_1" class="Level1">
<img src="images/frames/left/expands-collapses_plus.gif">
200
<div id="go_1_0" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_plus.gif">
210
<div id="go_1_0_0" class="Level3" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
211
</div>
</div>
<div id="go_1_1" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
220
</div>
<div id="go_1_2" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
230
</div>
<div id="go_1_3" class="Level2" style="display: none;">
<img src="images/frames/left/expands-collapses_spacer.gif">
240
</div>
</div>
As you can see very simple. Note how the id="" are go_0 (root) go_0_0 (first link) go_0_0_0 (first sub of first link) .... go_0_1 (second link) Now I just want to loop till there are no more subs. so. i call expand("go_0") what this should do is expand every go_0_x only, no go_0_x_x_x_x, just the first level. The actual question is how do I test go_0_0 is an actual object. so in the line: while(eval(doc + '[linkId_new]' + sty) == [Object]) how do i make it test whether eval(doc + '[linkId_new]' + sty) is a valid object?? |
|
#2
|
|||
|
|||
|
I just figured out a way to do this.
just after posting I remembered the try{} catch() {} block so the following works: Code:
function expandMenu(linkId) {
var x = 0;
linkId_new = linkId + "_0";
try {
while(1==1) {
linkId_new = linkId + "_" + x;
linkObj = eval(doc + '[linkId_new]' + sty);
linkObj.display = "block";
x++;
}
}
catch(Exception) {
}
}
|
|
#3
|
|||
|
|||
|
Is this the best way to do this or is there a better way??
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Programming Tools > Javascript: How to test whether an object is an object? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|