あなたが探している素晴らしいクロスブラウザ ソリューションは....存在しません... (大勢の人が「ああああ....」と言っている音を想像してみてください)。
DomContentLoadedは、ベスト ショットです。polling
IE-oldiesのテクニックはまだ必要です。
- addEventListener を使用してみてください。
- 利用できない場合 (明らかに IE)、フレームを確認します。
- フレームでない場合は、エラーがスローされなくなるまでスクロールします (ポーリング)。
- フレームの場合は、IE イベント document.onreadystatechange; を使用します。
- サポートされていないその他のブラウザーの場合は、古い document.onload イベントを使用してください。
すべてのブラウザーをカバーするために使用できるjavascript.infoで次のコード サンプルを見つけました。
function bindReady(handler){
var called = false
function ready() {
if (called) return
called = true
handler()
}
if ( document.addEventListener ) { // native event
document.addEventListener( "DOMContentLoaded", ready, false )
} else if ( document.attachEvent ) { // IE
try {
var isFrame = window.frameElement != null
} catch(e) {}
// IE, the document is not inside a frame
if ( document.documentElement.doScroll && !isFrame ) {
function tryScroll(){
if (called) return
try {
document.documentElement.doScroll("left")
ready()
} catch(e) {
setTimeout(tryScroll, 10)
}
}
tryScroll()
}
// IE, the document is inside a frame
document.attachEvent("onreadystatechange", function(){
if ( document.readyState === "complete" ) {
ready()
}
})
}
// Old browsers
if (window.addEventListener)
window.addEventListener('load', ready, false)
else if (window.attachEvent)
window.attachEvent('onload', ready)
else {
var fn = window.onload // very old browser, copy old onload
window.onload = function() { // replace by new onload and call the old one
fn && fn()
ready()
}
}
}