6 description: Contains the custom event domready.
8 license: MIT-style license.
10 requires: [Browser, Element, Element.Event]
12 provides: [DOMReady, DomReady]
17 (function(window, document){
24 testElement = document.createElement('div');
26 var domready = function(){
29 Browser.loaded = ready = true;
30 document.removeListener('DOMContentLoaded', domready).removeListener('readystatechange', check);
32 document.fireEvent('domready');
33 window.fireEvent('domready');
36 var check = function(){
37 for (var i = checks.length; i--;) if (checks[i]()){
44 var poll = function(){
46 if (!check()) timer = setTimeout(poll, 10);
49 document.addListener('DOMContentLoaded', domready);
52 // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
53 // testElement.doScroll() throws when the DOM is not ready, only in the top window
54 var doScrollWorks = function(){
56 testElement.doScroll();
61 // If doScroll works already, it can't be used to determine domready
63 if (testElement.doScroll && !doScrollWorks()){
64 checks.push(doScrollWorks);
69 if (document.readyState) checks.push(function(){
70 var state = document.readyState;
71 return (state == 'loaded' || state == 'complete');
74 if ('onreadystatechange' in document) document.addListener('readystatechange', check);
75 else shouldPoll = true;
77 if (shouldPoll) poll();
79 Element.Events.domready = {
81 if (ready) fn.call(this);
85 // Make sure that domready fires before load
86 Element.Events.load = {
89 if (loaded && this == window) fn.call(this);
91 condition: function(){
94 delete Element.Events.load;
100 // This is based on the custom load event
101 window.addEvent('load', function(){
105 })(window, document);