Skip to content

Instantly share code, notes, and snippets.

@ernestas-poskus
Forked from attilaolah/domready.coffee
Created February 3, 2017 12:02
Show Gist options
  • Select an option

  • Save ernestas-poskus/c8c5c3cccd949f1bb6118357d2b1526a to your computer and use it in GitHub Desktop.

Select an option

Save ernestas-poskus/c8c5c3cccd949f1bb6118357d2b1526a to your computer and use it in GitHub Desktop.

Revisions

  1. Attila Oláh revised this gist Jul 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion domready.coffee
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    ###

    # DOM ready event listener
    window.ready = (() ->
    window.ready = ( ->

    ready_event_fired = false

  2. Attila Oláh revised this gist Jul 18, 2013. 1 changed file with 57 additions and 0 deletions.
    57 changes: 57 additions & 0 deletions domready.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    ###
    # Based on:
    # jQuery JavaScript Library v1.4.2
    # http://jquery.com/
    #
    # Copyright 2010, John Resig
    # Dual licensed under the MIT or GPL Version 2 licenses.
    # http://jquery.org/license
    ###

    # DOM ready event listener
    window.ready = (() ->

    ready_event_fired = false

    (fn) ->

    # Create an idempotent version of the 'fn' function
    idempotent_fn = () ->
    unless ready_event_fired
    ready_event_fired = true
    fn()

    # The DOM ready check for Internet Explorer
    do_scroll_check = () ->
    # 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
    idempotent_fn()


    # If the browser ready event has already occurred
    if document.readyState is "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
    do_scroll_check() if document?.documentElement?.doScroll and window?.frameElement is null
    )()
  3. Attila Oláh revised this gist Jul 17, 2013. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions domready.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    /*!
    * Based on:
    * jQuery JavaScript Library v1.4.2
    * http://jquery.com/
    *
    * Copyright 2010, John Resig
    * Dual licensed under the MIT or GPL Version 2 licenses.
    * http://jquery.org/license
    */
    (function () {

    // DOM ready event listener
  4. Attila Oláh created this gist Jul 17, 2013.
    89 changes: 89 additions & 0 deletions domready.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    (function () {

    // DOM ready event listener
    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 && toplevel) {
    return do_scroll_check();
    }
    }
    };
    return ready_event_listener;
    })();

    // Put your own code here
    var ready_1 = function () {
    alert('foo');
    };
    var ready_2 = function () {
    alert('bar');
    };

    // Pass your functions to ready()
    ready(function () {
    ready_1();
    ready_2();
    });

    })();