tm-dom
Version:
Simple dom utilities
46 lines (45 loc) • 1.65 kB
JavaScript
;
/**
* the function calls the passed function after
* the document has finished loading and the document has been parsed,
* but sub-resources such as scripts, images, stylesheets and frames are still loading.
* @param {callback} () => {}
* @returns void
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState#value
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event
* @example documentReady(() => {})
*/
const documentReady = (callback) => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", callback, { once: true });
} else {
// call the passed function immediately
callback();
}
};
/**
* the function calls the passed function after
* the document and all sub-resources have finished loading.
* @param {callback} () => {}
* @returns void
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState#value
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readystatechange_event
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
* @example documentComplete(() => {})
*/
const documentComplete = (callback) => {
if (document.readyState !== "complete") {
const readyStateChangeHandler = (event) => {
const target = event.target;
if (target.readyState === "complete") {
callback();
}
};
document.addEventListener("readystatechange", readyStateChangeHandler);
} else {
// call the passed function immediately
callback();
}
};
export { documentReady, documentComplete };
//# sourceMappingURL=index.js.map