diff --git a/use.js b/use.js index 4397b0d..5d02345 100644 --- a/use.js +++ b/use.js @@ -32,6 +32,13 @@ export const run = (root = document) => { root.querySelectorAll("[use]").forEach(use) } +export const whenReady = () => { + if (document.readyState == "complete") + run(document) + else + document.addEventListener("readystatechange", whenReady, {once: true}) +} + export const install = () => { observe(document) run(document) diff --git a/use.md b/use.md index 341d583..086d3a9 100644 --- a/use.md +++ b/use.md @@ -43,11 +43,15 @@ use.observe(root=document) // child elements. Does not perform an initial run over // already existing elements. -use.use(element) -// Runs once on a selected element. -// Normally not recommended. - use.install() // Observes the entire document and performs // an initial scan over existing elements. + +use.whenReady() +// Waits for the document to be ready, then +// runs once on the whole document. + +use.use(element) +// Runs once on a selected element. +// Normally not recommended. ```