I'm making a non-jQuery-dependent plugin which, to play nicely with various existing popular jQuery features and plugins, must execute a function after the end of the DOM has been reached, but not after the execution of anything later queued using jQuery .ready()
.
The standard non-jQuery .ready()
alternative is document.addEventListener("DOMContentLoaded",function(){});
.
If a page has functions deferred using both jQuery .ready()
and DOMContentLoaded
:
- all the jQuery
.ready()
s execute in the order they were defined - ...then all the
DOMContentLoaded
s execute, in the order they were defined:
...and both occur between the interactive
and complete
document readyStates:
document.addEventListener("readystatechange", function () { alert(document.readyState); });
document.addEventListener("DOMContentLoaded", function(event) { alert(1); });
$(document).ready(function(){ alert(2); });
document.addEventListener("DOMContentLoaded", function(event) { alert(3); });
$(document).ready(function(){ alert(4); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
What can I use which is like document.addEventListener("DOMContentLoaded")
but is not pre-empted by any and all jQuery .ready()
s like this?
To be clear about the exact timing, in my particular case, I can guarantee that this time-crucial code is queued before any .ready()
s are queued, so answers which append to the same queue as .ready()
will also solve my problem, not just answers which pre-empt all .ready()
s. But pre-empting all .ready()
s is probably preferable, since it would be applicable for a wider range of cases.
(bonus points for any clear simple explanation as to what it is about jQuery's .ready()
that causes it to execute before all document.addEventListener("DOMContentLoaded")
regardless of when they were defined, I can't figure it out)