Skip to content

Instantly share code, notes, and snippets.

@terry-fei
Created November 20, 2015 11:41
Show Gist options
  • Select an option

  • Save terry-fei/90c62c7fc034a22f52a6 to your computer and use it in GitHub Desktop.

Select an option

Save terry-fei/90c62c7fc034a22f52a6 to your computer and use it in GitHub Desktop.

Revisions

  1. Teng Fei created this gist Nov 20, 2015.
    30 changes: 30 additions & 0 deletions domReady
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /*!
    * domready (c) Dustin Diaz 2012 - License MIT
    */
    var domready = (function domready() {
    var fns = [],
    listener,
    doc = document,
    hack = doc.documentElement.doScroll,
    domContentLoaded = 'DOMContentLoaded',
    loaded = (hack ? /^loaded|^c/ : /^loaded|^i|^c/).test(doc.readyState);


    if (!loaded) {
    doc.addEventListener(domContentLoaded, listener = function () {
    doc.removeEventListener(domContentLoaded, listener);
    loaded = 1;
    while (listener = fns.shift()) { // jshint ignore:line
    listener();
    }
    });
    }

    return function (fn) {
    if (loaded) {
    setTimeout(fn, 0);
    } else {
    fns.push(fn);
    }
    };
    })();