views:

3327

answers:

2

Hey there

I'm having trouble with some JS in IE7. I'm testing to see if a certain object has a className (its possibly an HTMLElement object from the DOM) assigned.

Now, testing the page in Firefox tells me that Yes, the variable is undefined (all my tests below do the Alert().

In IE, none of the tests pass, the variable gets assigned on the last IF statement, and during the last Alert() IE chucks an "className is null or not an object" error, based on the fn_note.className statement.

Here's the code:

        var fn_note;
        var kids = area.childNodes;
        for (var l = 0; l < kids.length; l++){
            //DEBUG check if the found var exists
            if (kids[l].className == null){
                //then the className var doens't exist
                alert ('the classsname for the following var is null: --'+kids[l]+'--');
            }
            if (kids[l].className == undefined){
                //then the className var doens't exist
                alert ('the classsname for the following var is undefined: --'+kids[l]+'--');
            }                    
            if (kids[l].className == ''){
                //then the className var doens't exist
                alert ('the classsname for the following var is an empty string: --'+kids[l]+'--');
            }
            if (typeof kids[l].className === 'undefined'){
                //then the className var doens't exist
                alert ('the classsname for the following var is NEW TYPEOF TEST: --'+kids[l]+'--');
            }                      

            if (kids[l].className == 'fn-note') { /* (/fn-note$/).test(kids[l].className) IE doesn't really like regex. por supuesto */
                //we have found the div we want to hide
                fn_note = kids[l];                       
            }
        }                    
        alert('the clicked on className is '+area.className+'name of the found div is '+fn_note.className);

Please let me know what I am doing wrong. I know its probably something basic but I just can't see it ATM.

Thanks in advance.

+2  A: 

I think that what you get from the property is not a string, but an object of the type 'undefined'. Try this:

if (typeof(kids[l].className) == 'undefined') {
Guffa
That code is already in the example
pope
Well, not exactly. That code does an additional type check on the type string which is not needed at all and may actually cause problems. There is a literal string type and there is a String object, and they may not evaluate as the same type.
Guffa
thanks for you help as well.
mieze
+1  A: 

As far as I can see the only thing you really want to know is if there's a childNode with className 'fn-note' in the childNodes collection. So do a somewhat more rigourous test:

for (var l = 0; l < kids.length; l++){
    if (kids[l] 
        && kids[l].className 
        && kids[l].className.match(/fn\-note$/i)) {
       fn_note =  kids[l];
    }
}

This should be sufficient (do mind escaping the dash in the regexp).

KooiInc