Skip to content

Instantly share code, notes, and snippets.

@sathomas
Created July 22, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sathomas/5d854cc139d84168e2eed66c52a58822 to your computer and use it in GitHub Desktop.
Save sathomas/5d854cc139d84168e2eed66c52a58822 to your computer and use it in GitHub Desktop.
Document Ready
// ### Ready
//
// This module provides a convenient way to register
// a callback when the document is ready. The callback
// is called immediately if the document is ready
// when the module's API is called.
//
// > This really shouldn't be necessary as a library
// module, but IE10 and below have a nasty bug in how
// they report `document.readyState`. The workaround
// is pretty kludgy, so better to keep it confined
// to one bit of code instead of letting it proliferate.
//
// API:
//
// - `Intellum.util.ready(callback)` registers
// a callback function.
;(function(document, window) {
'use strict';
// Use the `Intellum.util` namespace.
window.Intellum = window.Intellum || {};
window.Intellum.util = window.Intellum.util || {};
// State variables for all instances
var isHandlerInstalled = false,
callbacks = [];
window.Intellum.util.ready = (function(callback) {
// A local event handler
function documentIsReady() {
// Now that the document is ready, we
// can remove our event handlers.
document.removeEventListener("DOMContentLoaded", documentIsReady);
window.removeEventListener("load", documentIsReady);
isHandlerInstalled = false;
// Trigger any callbacks
callbacks.forEach(function(callback) {
callback();
});
// Make sure we don't trigger any
// callback more than once.
callbacks = [];
}
// Original jQuery code to workaround the IE bug:
// https://github.com/jquery/jquery/pull/901
// We're pretty much doing the same thing.
// The `doScroll` method was proprietary
// to Internet Explorer and removed in IE11.
// https://msdn.microsoft.com/en-us/library/ms536414(v=vs.85).aspx
if ( document.readyState === "complete" ||
(document.readyState !== "loading" &&
!document.documentElement.doScroll) ) {
// Trigger the callback at the end of the
// execution stack in case any caller
// is doing something funky that would
// cause a race condition, i.e. if they're
// not completely ready for the callback.
window.setTimeout(callback, 0);
} else {
// The document isn't ready yet, so
// remember this callback.
callbacks.push(callback);
// Have we installed our own ready state
// handler yet? If not, we'll need to
// do so now.
if (!isHandlerInstalled) {
// Only do this once.
isHandlerInstalled = true;
// The best event to use is "DOMContentLoaded"
document.addEventListener("DOMContentLoaded", documentIsReady);
// But just in case that event is funky
// (might be in some browsers), also use
// the "load" event as a fallback.
window.addEventListener("load", documentIsReady);
}
}
});
})(window.document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment