好友怿飞前不久总结过:模拟兼容性的 addDOMLoadEvent 事件

需要特别注意:在 iframe 中,doScroll 方法不会抛异常。需要用 onreadystatechange 来模拟:

// from jQuery 1.3.2
// If IE event model is used
} else if ( document.attachEvent ) {
    // ensure firing before onload,
    // maybe late but safe also for iframes
    document.attachEvent("onreadystatechange", function() {
        if ( document.readyState === "complete" ) {
            // ...
        }
    });

    // If IE and not an iframe
    // continually check to see if the document is ready
    if ( document.documentElement.doScroll && window == window.top ) (function() {
        // doScroll ...
    })();
}

YUI 用户要小心:目前在 YUI 2.7 和 YUI 3.0 pb1 中,均未修复此 bug. 在 iframe 中,请尽量将 js 代码放在文件尾部以避免此问题。

在 YUI 2.8 中,该 bug 已修复:Ticket #2008289. 从 bug 被提交,到问题解决,历时一年多,囧。