Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS & CoffeeScript DOM ready listeners
###
# 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
)()
/*!
* 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
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();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment