I inherited this piece of code, and it seems suboptimal and possibly wrong, as it adds event listeners to both the window and the document objects. However, it works correctly, with the exception of Blackberry 5.0. Can someone explain if this is all set up correctly or are there 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);
javascript javascript-events onload-event
VinnyD Aug 01 2018-11-11T00: 00Z
source share