mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-02 07:21:05 +00:00
Track DOM content loaded for ready() function (#1688)
Track whether DOMContentLoaded has been fired
This commit is contained in:
parent
bfd7965dec
commit
93bd81b6d0
15
src/htmx.js
15
src/htmx.js
@ -3603,9 +3603,22 @@ return (function () {
|
|||||||
//====================================================================
|
//====================================================================
|
||||||
// Initialization
|
// Initialization
|
||||||
//====================================================================
|
//====================================================================
|
||||||
|
var isReady = false
|
||||||
|
getDocument().addEventListener('DOMContentLoaded', function() {
|
||||||
|
isReady = true
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a function now if DOMContentLoaded has fired, otherwise listen for it.
|
||||||
|
*
|
||||||
|
* This function uses isReady because there is no realiable way to ask the browswer whether
|
||||||
|
* the DOMContentLoaded event has already been fired; there's a gap between DOMContentLoaded
|
||||||
|
* firing and readystate=complete.
|
||||||
|
*/
|
||||||
function ready(fn) {
|
function ready(fn) {
|
||||||
if (getDocument().readyState === 'complete') {
|
// Checking readyState here is a failsafe in case the htmx script tag entered the DOM by
|
||||||
|
// some means other than the initial page load.
|
||||||
|
if (isReady || getDocument().readyState === 'complete') {
|
||||||
fn();
|
fn();
|
||||||
} else {
|
} else {
|
||||||
getDocument().addEventListener('DOMContentLoaded', fn);
|
getDocument().addEventListener('DOMContentLoaded', fn);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user