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);
31 document.fireEvent('domready');
32 window.fireEvent('domready');
35 document = window = testElement = null;
38 var check = function(){
39 for (var i = checks.length; i--;) if (checks[i]()){
46 var poll = function(){
48 if (!check()) timer = setTimeout(poll, 10);
51 document.addListener('DOMContentLoaded', domready);
54 // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
55 // testElement.doScroll() throws when the DOM is not ready, only in the top window
56 var doScrollWorks = function(){
58 testElement.doScroll();
63 // If doScroll works already, it can't be used to determine domready
65 if (testElement.doScroll && !doScrollWorks()){
66 checks.push(doScrollWorks);
71 if (document.readyState) checks.push(function(){
72 var state = document.readyState;
73 return (state == 'loaded' || state == 'complete');
76 if ('onreadystatechange' in document) document.addListener('readystatechange', check);
77 else shouldPoll = true;
79 if (shouldPoll) poll();
81 Element.Events.domready = {
83 if (ready) fn.call(this);
87 // Make sure that domready fires before load
88 Element.Events.load = {
91 if (loaded && this == window) fn.call(this);
93 condition: function(){
96 delete Element.Events.load;
102 // This is based on the custom load event
103 window.addEvent('load', function(){
107 })(window, document);