diff options
author | Alexander Petros <apetros15@gmail.com> | 2023-11-30 17:15:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-30 15:15:51 -0700 |
commit | 40cd1a5e3fa528ad44e01a7d773448c1505b3735 (patch) | |
tree | ebaaa142e9814ba4c93bef1af28a5daba710c566 /src/htmx.js | |
parent | 8d0338ade842fac0158b41c389cd26ac6ad60a8b (diff) | |
download | htmx-40cd1a5e3fa528ad44e01a7d773448c1505b3735.tar.gz htmx-40cd1a5e3fa528ad44e01a7d773448c1505b3735.zip |
Revert readystate PR (#2040)
Revert "Fix for race condition in readystate detection (#1972)"
This reverts commit 78a9ecf17079fcdfa3d674700d8338b60f14a058.
Diffstat (limited to 'src/htmx.js')
-rw-r--r-- | src/htmx.js | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/src/htmx.js b/src/htmx.js index 1f0709d7..82499875 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -3752,34 +3752,25 @@ return (function () { //==================================================================== // Initialization //==================================================================== + var isReady = false + getDocument().addEventListener('DOMContentLoaded', function() { + isReady = true + }) + /** - * We want to initialize the page elements after DOMContentLoaded - * fires, but there isn't always a good way to tell whether - * it has already fired when we get here or not. + * 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(functionToCall) { - // call the function exactly once no matter how many times this is called - var callReadyFunction = function() { - if (!functionToCall) return; - functionToCall(); - functionToCall = null; - }; - - if (getDocument().readyState === "complete") { - // DOMContentLoaded definitely fired, we can initialize the page - callReadyFunction(); - } - else { - /* DOMContentLoaded *maybe* already fired, wait for - * the next DOMContentLoaded or readystatechange event - */ - getDocument().addEventListener("DOMContentLoaded", function() { - callReadyFunction(); - }); - getDocument().addEventListener("readystatechange", function() { - if (getDocument().readyState !== "complete") return; - callReadyFunction(); - }); + function ready(fn) { + // 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(); + } else { + getDocument().addEventListener('DOMContentLoaded', fn); } } |