Invoke `doAfterLoad` even if page is loaded

Fixes #3.
This commit is contained in:
Florine W. Dekker 2021-04-13 20:12:52 +02:00
parent 5043c92d56
commit 79b68fdb2c
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
2 changed files with 8 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@fwdekker/template", "name": "@fwdekker/template",
"version": "0.0.26", "version": "0.0.27",
"description": "The base template for pages on fwdekker.com.", "description": "The base template for pages on fwdekker.com.",
"author": "Felix W. Dekker (https://fwdekker.com)", "author": "Felix W. Dekker (https://fwdekker.com)",
"license": "MIT", "license": "MIT",

View File

@ -27,11 +27,17 @@ export const $ = q => document.querySelector(q);
/** /**
* Runs the given function once the page is loaded. * Runs the given function once the page is loaded.
* *
* This function can be used multiple times. It does not overwrite existing callbacks for the page load event. * This function can be used multiple times. It does not overwrite existing callbacks for the page load event. If the
* page has already loaded when this function is invoked, `fun` is invoked immediately inside this function.
* *
* @param fun {function(...*): *} the function to run * @param fun {function(...*): *} the function to run
*/ */
export const doAfterLoad = function (fun) { export const doAfterLoad = function (fun) {
if (document.readyState === "complete") {
fun();
return;
}
const oldOnLoad = window.onload || (() => { const oldOnLoad = window.onload || (() => {
}); });