Skip to main content Accessibility Feedback


Run event after the DOM is ready.

Works in all modern browsers, and at least back to IE9.

 * Run event after the DOM is ready
 * (c) 2017 Chris Ferdinandi, MIT License,
 * @param  {Function} fn Callback function
var ready = function (fn) {

	// Sanity check
	if (typeof fn !== 'function') return;

	// If document is already loaded, run method
	if (document.readyState === 'interactive' || document.readyState === 'complete') {
		return fn();

	// Otherwise, wait until document is loaded
	document.addEventListener('DOMContentLoaded', fn, false);