views:

133

answers:

2

This is in continuation of the question:

http://stackoverflow.com/questions/1801960/innerhtml-working-in-ff-but-not-in-ie

The above problem is resolved [ thanx to pygorex1 ] . But i would like to know why the following code snippet is not working.

if (window.addEventListener){   
    window.addEventListener('load', addDateFormatInfo, false); 
    window.addEventListener('load', loadNewElements, false); 
} else if (window.attachEvent){ 
    window.attachEvent('onload', addDateFormatInfo); 
    window.attachEvent('onload', loadNewElements);
}

function loadNewElements(){
    document.createElement("showDateFormat");
}

function addDateFormatInfo(){
    var dateFormatHolder = document.getElementsByTagName("showDateFormat");     
    if ( dateFormatHolder ){            
        for ( i = 0 ; i < dateFormatHolder.length; i++ ){
                dateFormatHolder[i].innerHTML = '<div class="infoSmall" ><span>(mm/dd/yyyy)</span></div>';                                               
        } 
    }
}

provided it is working in FF but not in IE
also, if instead of creating a new method loadNewElements and attaching this to load the event if i will directly write document.createElement("showDateFormat"); in my javascript [ not in any method ], then the code works as expected [ both in IE and FF ]. Why it is so?

+2  A: 

You need to call loadNewElements before the other method.

If it doesn't work, write like this :

if (window.addEventListener){   
    window.addEventListener('load', addDateFormatInfo, false);  
    //window.addEventListener('load', loadNewElements, false);    // Don't need this for non-IE browsers
} else if (window.attachEvent){ 
    loadNewElements(); // Will always be executed before the other one
    window.attachEvent('onload', addDateFormatInfo);    
}
Fabien Ménager
i have already placed `window.addEventListener('load', loadNewElements, false);` and if i keep an `alert` in `loadNewElements` method it get executed before `addDateFormatInfo`. Even then in IE it is not working
Rakesh Juyal
Do you place "window.attachEvent('onload', loadNewElements);" before the other attachEvent call ?
Fabien Ménager
@Fabien: see the code, i have already placed 'loadNewElements' just after attaching the 'addDateFormatInfo', so `loadNewElements` is always getting executed before `addDateFormatInfo`
Rakesh Juyal
Why would it be executed before the other one ? It is attached after ... Did you try to swap the two lines ?
Fabien Ménager
Well, the docs are pretty explicit : "If you attach multiple functions to the same event on the same object, the functions are called in random order, immediately after the object's event handler is called." http://msdn.microsoft.com/en-us/library/ms536343%28VS.85%29.aspx You don't which one is executed before the other one when using attachEvent.
Fabien Ménager
+2  A: 

I'd try doing this:

if (window.addEventListener){   
  window.addEventListener('load', function() {loadNewElements(); addDateFormatInfo()}, false);  
} else if (window.attachEvent){ 
  window.attachEvent('onload', function() {loadNewElements(); addDateFormatInfo()});    
}
Mr. Shiny and New
i will try this, by tht time it is +1 ;)
Rakesh Juyal