I inherited this piece of code and it seems sub-optimal and possibly incorrect since it's adding event listeners on both the window and document objects. However, it is working properly except for blackberry 5.0. Can someone explain if all this is set up correctly or if there are any recommendations to make it better and/or more streamlined?
if (document.readyState === "complete")
callback();
else if (document.addEventListener)
{
document.addEventListener("DOMContentLoaded",callback,false);
window.addEventListener("load",callback,false);
}
else if(window.attachEvent)
{
document.attachEvent("onreadystatechange", callback);
window.attachEvent("onLoad",callback);
} else
setTimeout(callback,2000);
DOMContentLoaded
or the way i proposed because the execution of this async. script may occur after this event fired or the HTML-Parser reaches the bottom of the html file. Therefore onlywindow.onload
andxhr.onreadystatechange
can be used - both is cross browser compatible. – Beichner