Modern DOM ready utility for JavaScript applications
npx @tessl/cli install tessl/npm-domready@1.0.0DOMReady is a lightweight, modern DOM ready utility for JavaScript applications that enables developers to execute code after the DOM has finished loading but before all images and stylesheets are complete. It provides cross-browser compatibility for modern browsers with zero dependencies.
npm install domreadyFor CommonJS (Node.js):
const domready = require("domready");For AMD (RequireJS):
define(['domready'], function(domready) {
// Use domready
});For global usage (browser):
<script src="path/to/domready.js"></script>
<script>
// domready is available globally
</script>Note: This package uses UMD (Universal Module Definition) and does not provide native ES module exports. If using with modern bundlers like Webpack or Rollup, they will typically handle the CommonJS import automatically.
const domready = require("domready");
// Execute code when DOM is ready
domready(function() {
console.log("DOM is ready!");
document.getElementById('myElement').style.display = 'block';
});
// Multiple callbacks can be registered
domready(function() {
// First callback
});
domready(function() {
// Second callback - will also execute when DOM is ready
});Registers callback functions to execute when the DOM has finished loading but before all external resources (images, stylesheets) are complete.
/**
* Registers a callback function to execute when DOM is ready
* @param {Function} fn - Callback function to execute when DOM is ready
* @returns {void}
*/
function domready(fn) {
// Implementation provided by UMD module
}Behavior:
setTimeout(fn, 0)DOMContentLoaded event firesDOMContentLoaded event with automatic cleanup to prevent memory leaksBrowser Compatibility:
Usage Examples:
// Basic DOM manipulation
domready(function() {
const element = document.getElementById('content');
if (element) {
element.innerHTML = 'Page loaded!';
}
});
// Multiple independent callbacks
domready(function() {
// Initialize navigation
setupNavigation();
});
domready(function() {
// Initialize analytics
trackPageView();
});
// With arrow functions (ES6+)
domready(() => {
document.body.classList.add('loaded');
});When used with the Ender.js framework, domready provides additional jQuery-like methods.
// Direct access to domready function
$.domReady; // Function
/**
* jQuery-like ready method that returns the Ender chain for chaining
* @param {Function} f - Callback function to execute when DOM is ready
* @returns {Object} - Returns 'this' for method chaining
*/
$.ready(f) {
// Returns this for method chaining
}Ender.js Usage Examples:
// Using domReady directly
require('domready')(function() {
$('body').html('<p>DOM ready via require</p>');
});
// Using jQuery-like syntax
$(document).ready(function() {
$('body').html('<p>DOM ready via $.ready</p>');
});
// Method chaining with ready
$(document)
.ready(function() {
// DOM is ready
})
.addClass('initialized');DOMReady supports multiple module systems through Universal Module Definition (UMD):
module.exports = domreadydomready as a global variable when no module system is detectedDOMContentLoaded) with cross-browser compatibility