Internet Explorer 7 – Javascript 'undefined' not testing

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().

  • Is there a static check for native function support across browsers?
  • Resources for looking up differences in rendering behavior between web-browsers and browser bugs
  • HTML 4 equivalent of HTML 5's FileReader?
  • What is the difference between “javascript:”, “#”, and “javascript:false” in iframe.src attribute
  • Show datalist labels but submit the actual value
  • Cross-browser window resize event - JavaScript / jQuery
  • 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.

  • What is minimum millisecond value of setTimeout?
  • window.setTimeout behaviour when window not in focus
  • Nivo-Slider disappears while slide is being changed in IE ≤ 8
  • Prevent zoom cross-browser
  • Does Chrome not support prototyped properties in Javascript?
  • Detecting individual Unicode character support with JavaScript
  • 2 Solutions collect form web for “Internet Explorer 7 – Javascript 'undefined' not testing”

    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).

    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') {