title: $(document).ready() source
body: <p>I need to wait for document readyness in my JavaScript, to insert a div at the bottom of the body.</p>
  
  <p>I want to:</p>
  
  <ul>
  <li>make this JavaScript file as small as possible (compile it down to &lt; 1kb if possible)</li>
  <li>inline the code that provides the document readyness in a closure (without exporting it)</li>
  </ul>
  
  <p>Inlining the whole jQuery source in my file would be too big, so I'm looking for other methods. <code>window.onload</code> would work, but I specifically want document readyness, and not wait for the <code>window.onload</code> event.</p>
  
  <p>Does anyone know a JS snippet that can do this? Or should I just copy part of jQuery's source?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I managed to crawl the jQuery source and put together with the following snippet:</p>
  
  <pre><code>var ready = (function () {
      var ready_event_fired = false;
      var ready_event_listener = function (fn) {
  
          // Create an idempotent version of the 'fn' function
          var idempotent_fn = function () {
              if (ready_event_fired) {
                  return;
              }
              ready_event_fired = true;
              return fn();
          }
  
          // The DOM ready check for Internet Explorer
          var do_scroll_check = function () {
              if (ready_event_fired) {
                  return;
              }
  
              // If IE is used, use the trick by Diego Perini
              // http://javascript.nwbox.com/IEContentLoaded/
              try {
                  document.documentElement.doScroll('left');
              } catch(e) {
                  setTimeout(do_scroll_check, 1);
                  return;
              }
  
              // Execute any waiting functions
              return idempotent_fn();
          }
  
          // If the browser ready event has already occured
          if (document.readyState === "complete") {
              return idempotent_fn()
          }
  
          // Mozilla, Opera and webkit nightlies currently support this event
          if (document.addEventListener) {
  
              // Use the handy event callback
              document.addEventListener("DOMContentLoaded", idempotent_fn, false);
  
              // A fallback to window.onload, that will always work
              window.addEventListener("load", idempotent_fn, false);
  
          // If IE event model is used
          } else if (document.attachEvent) {
  
              // ensure firing before onload; maybe late but safe also for iframes
              document.attachEvent("onreadystatechange", idempotent_fn);
  
              // A fallback to window.onload, that will always work
              window.attachEvent("onload", idempotent_fn);
  
              // If IE and not a frame: continually check to see if the document is ready
              var toplevel = false;
  
              try {
                  toplevel = window.frameElement == null;
              } catch (e) {}
  
              if (document.documentElement.doScroll &amp;&amp; toplevel) {
                  return do_scroll_check();
              }
          }
      };
      return ready_event_listener;
  })();
  
  // TEST
  var ready_1 = function () {
      alert("ready 1");
  };
  var ready_2 = function () {
      alert("ready 2");
  };
  ready(function () {
      ready_1();
      ready_2();
  });
  </code></pre>
  
  <p>Thank you very much for helping me find this in the jQuery source. I can now put all this in a closure and do my work without exporting any functions and polluting the global scope.</p>