vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
22 lines (21 loc) • 562 B
JavaScript
import once from './once';
export function docComplete() {
const { readyState } = document;
return readyState === 'interactive' || readyState === 'complete';
}
/**
* Execute a given function once the document has finished loading
*
* @param handler - Function to execute once the document has finished loading
*
* @example
*
* ```ts
* domReady((e: Event) => { alert('DOM Ready') });
* ```
*/
export default function domReady(handler) {
docComplete()
? handler()
: once('readystatechange', handler, { when: docComplete });
}