Helpful Information
 
 
Category: DOM and JSON scripting
why does my code run only in IE?

What's wrong with this code?
When I run JavaScript Console in Mozilla it says:"E has no properties".

The code:




--------------------------------------------------------------------------------------
function razsiri(el, E) {
preventBubble(E);
if (el.open) {
for (var i = 0; i < el.childNodes.length; i++) {
if (el.childNodes[i].nodeType != 3) {
el.childNodes[i].style.display = 'none';}}
el.open = false;
}
else{
for (var i = 0; i < el.childNodes.length; i++) {
if (el.childNodes[i].nodeType != 3) {
el.childNodes[i].style.display = 'block';}}
el.open = true;
}
}

preventBubble = function(E){
if(isIE){
event.cancelBubble=true;
event.returnValue=false;
}else{
if(E.stopPropagation)E.stopPropagation();
else E.preventBubble();
}
};
-------------------------------------------------------------------------------------

The explanation of the code:

It's like Windows Explorer with folders in the left.
I have a "root" <span> (like My Computer :) ) and several <span>s inside the "My Computer" <span> (they have a display: none), and more <span>s nested inside those.

When you click on a span, the browser checks if the <span> contains (an) element(s). If it does, it sets its display: block and thus shows it.

how did you call razsiri() function? it should be:

<span id="spanID" onclick="razsiri(this, event)">blah blah</span>

Here's the link: link (http://www2.arnes.si/~rcarl/Explorer%20varna.html).

all_spans.onclick = function() {razsiri(this, event)}

Originally posted by Rok
Here's the link: link (http://www2.arnes.si/~rcarl/Explorer%20varna.html).

all_spans.onclick = function() {razsiri(this, event)}

all_spans.onclick = function(event) {razsiri(this, event)}

Great, it works...

Damn, I really suck with this event thing... I learned JavaScript from a book that did not contained little info about event and on top of it I didn't understand a damn thing because I was just a newbie in JavaScript then. I think I should read through that chapter again. And this time read it thoroughly...

Thanx!










privacy (GDPR)