/*
*
* ContentLoaded.js
*
* Author: Diego Perini (diego.perini at gmail.com)
* Summary: Cross-browser wrapper for DOMContentLoaded
* Updated: 05/10/2007
* License: GPL/CC
* Version: 1.0
*
* Notes:
*
* based on code by Dean Edwards and John Resig
* http://dean.edwards.name/weblog/2006/06/again/
* http://javascript.nwbox.com/ContentLoaded/
*
*/
// @w window reference
// @f function reference
function ContentLoaded (w, fn) {
var d = w.document,
u = w.navigator.userAgent.toLowerCase();
function init(e) {
if (!arguments.callee.done) {
arguments.callee.done = true;
fn(e);
}
}
// konqueror/safari
if (/khtml|webkit/.test(u)) {
(function () {
if (/complete|loaded/.test(d.readyState)) {
init('poll');
} else {
setTimeout(arguments.callee, 10);
}
})();
// internet explorer all versions
} else if (/msie/.test(u) && !w.opera) {
(function () {
try {
d.documentElement.doScroll('left');
} catch (e) {
setTimeout(arguments.callee, 10);
return;
}
init('poll');
})();
d.attachEvent('onreadystatechange',
function (e) {
if (d.readyState == 'complete') {
d.detachEvent('on'+e.type, arguments.callee);
init(e.type);
}
}
);
// browsers having native DOMContentLoaded
} else if (d.addEventListener &&
(/gecko/.test(u) && parseFloat(u.split('rv:')[1]) >= 1.8) ||
(/opera/.test(u) && parseFloat(u.split('opera ')[1]) > 9)) {
d.addEventListener('DOMContentLoaded',
function (e) {
this.removeEventListener(e.type, arguments.callee, false);
init(e.type);
}, false
);
// fallback to last resort
} else {
// from Simon Willison
var oldonload = w.onload;
w.onload = function (e) {
if (typeof oldonload == 'function') {
oldonload(e || w.event);
}
init((e || w.event).type);
};
}
}
