events_ready.js

/**
 * @module ready
 * @description Fires a function when the document is ready.
 *
 * @since 1.0.0
 *
 * @param {Function} fn The function to fire on document ready.
 *
 * @return {void}
 *
 * @example
 * import { ready } from "@gravityforms/utils";
 *
 * function Example() {
 *     // do something on document ready
 * };
 *
 * ready( Example );
 *
 */
export default function ready( fn ) {
	if ( document.readyState !== 'loading' ) {
		fn();
	} else if ( document.addEventListener ) {
		document.addEventListener( 'DOMContentLoaded', fn );
	} else {
		document.attachEvent( 'onreadystatechange', () => {
			if ( document.readyState !== 'loading' ) {
				fn();
			}
		} );
	}
}