IEContentLoaded (last updated 05/10/2007)
An alternative for DOMContentLoaded on Internet Explorer
This is an Internet Explorer only method. It does not produce the expected results on other browsers.
It is meant to be used as an IE alternative in other fine scripts trying to fix the window onload problem.
It is based on a hack (no doubt about that), but I hope it is an improvement over previous IE hacks.
Several tests has showed that the only other (documented)
method covering all the aspects of this complex problem is the original Microsoft HTC method as described by Dean Edwards
in An Alternative Solution.
However an inline solution is always preferred, possibly XHTML compatible, with no document.write and without requiring external files.
The solution I show here uses the "doScroll()" method in a try/catch polling loop. This triggers when no errors are returned by "doScroll().
You can quickly download the small iecontentloaded.js script or just have a look at it.
The Microsoft online documentation About Element Behaviors tell us something about this:
When the ondocumentready event fires, the document has been completely parsed and built.
Initialization code should be placed here if the component needs to navigate the primary document structure.
The ondocumentready event notifies the component that the entire page is loaded, and it fires
immediately before the onload event fires in the primary document.
A few methods, such as doScroll, require the primary document to be completely loaded.
If these methods are part of an initialization function, they should be handled when the ondocumentready event fires.
Some TEST CASES showing the IEContentLoaded method
The objective of these tests is to compare the detection reliability of this method.
In this specific case we are interested in determining when all the nodes in the
requested page have been loaded and parsed in the DOM and available to scripts.
The fact that images exists or not in the page shouldn't affect this test.
The test case executes these steps, in sequence, at different times:
- INLINE STYLE is applied and the BODY background color is set to "lime"
- Ending BODY SCRIPT is executed and the background color is set to "red"
- IEContentLoaded function is fired and the background color is set to "green"
The page should have a "green" background color after being loaded completely.
If that is not the case then something went wrong in my script or in your browser.
The IEContentLoaded source code
/*
*
* IEContentLoaded.js
*
* Author: Diego Perini (diego.perini at gmail.com) NWBOX S.r.l.
* Summary: DOMContentLoaded emulation for IE browsers
* Updated: 05/10/2007
* License: GPL
* Version: TBD
*
* Copyright (C) 2007 Diego Perini & NWBOX S.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://javascript.nwbox.com/IEContentLoaded/GNU_GPL.txt.
*
*/
// @w window reference
// @fn function reference
function IEContentLoaded (w, fn) {
var d = w.document, done = false,
// only fire once
init = function () {
if (!done) {
done = true;
fn();
}
};
// polling for no errors
(function () {
try {
// throws errors until after ondocumentready
d.documentElement.doScroll('left');
} catch (e) {
setTimeout(arguments.callee, 50);
return;
}
// no errors, fire
init();
})();
// trying to always fire before onload
d.onreadystatechange = function() {
if (d.readyState == 'complete') {
d.onreadystatechange = null;
init();
}
};
}
Fine script for the window.onload problem
This filler ensure exceeding the default 4Kb output caching of Apache.
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
This filler ensure exceeding the default 4Kb output caching of Apache.
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
This filler ensure exceeding the default 4Kb output caching of Apache.
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
This filler ensure exceeding the default 4Kb output caching of Apache.
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test
test test testtest test testtest test testtest test testtest test testtest test test