1//Document ready function (Vanilla
2(function(funcName, baseObj) {
3    // The public function name defaults to window.docReady
4    // but you can pass in your own object and own function name and those will be used
5    // if you want to put them in a different namespace
6    funcName = funcName || "docReady";
7    baseObj = baseObj || window;
8    var readyList = [];
9    var readyFired = false;
10    var readyEventHandlersInstalled = false;
11
12    // call this when the document is ready
13    // this function protects itself against being called more than once
14    function ready() {
15        if (!readyFired) {
16            // this must be set to true before we start calling callbacks
17            readyFired = true;
18            for (var i = 0; i < readyList.length; i++) {
19                // if a callback here happens to add new ready handlers,
20                // the docReady() function will see that it already fired
21                // and will schedule the callback to run right after
22                // this event loop finishes so all handlers will still execute
23                // in order and no new ones will be added to the readyList
24                // while we are processing the list
25                readyList[i].fn.call(window, readyList[i].ctx);
26            }
27            // allow any closures held by these functions to free
28            readyList = [];
29        }
30    }
31
32    function readyStateChange() {
33        if ( document.readyState === "complete" ) {
34            ready();
35        }
36    }
37
38    // This is the one public interface
39    // docReady(fn, context);
40    // the context argument is optional - if present, it will be passed
41    // as an argument to the callback
42    baseObj[funcName] = function(callback, context) {
43        if (typeof callback !== "function") {
44            throw new TypeError("callback for docReady(fn) must be a function");
45        }
46        // if ready has already fired, then just schedule the callback
47        // to fire asynchronously, but right away
48        if (readyFired) {
49            setTimeout(function() {callback(context);}, 1);
50            return;
51        } else {
52            // add the function and context to the list
53            readyList.push({fn: callback, ctx: context});
54        }
55        // if document already ready to go, schedule the ready function to run
56        if (document.readyState === "complete") {
57            setTimeout(ready, 1);
58        } else if (!readyEventHandlersInstalled) {
59            // otherwise if we don't have event handlers installed, install them
60            if (document.addEventListener) {
61                // first choice is DOMContentLoaded event
62                document.addEventListener("DOMContentLoaded", ready, false);
63                // backup is window load event
64                window.addEventListener("load", ready, false);
65            } else {
66                // must be IE
67                document.attachEvent("onreadystatechange", readyStateChange);
68                window.attachEvent("onload", ready);
69            }
70            readyEventHandlersInstalled = true;
71        }
72    }
73})("docReady", window);
74