[ACCEPTED]-Running a function just before $(document).ready() triggers-document-ready
This would be a time where I would trigger 7 a custom event that all of your other files 6 would bind
to, you would only have one ready
handler, which 5 would do stuff, then trigger the custom 4 event.
So, somewhere else in your code, instead 3 of using $(document).ready
, you would use
$(window).bind('setup', function() {
//code here...
});
And/or
$(window).bind('loaded', function() {
//some other code here...
});
And in your 2 one and only ready
handler, you'd do something 1 like this:
$(document).ready(function() {
$(window).trigger('setup');
$(window).trigger('loaded'):
});
$(document).ready()
or simply $(function(){})
is just an .addEventListener
implementation (well 4 not exactly, but close), meaning you can 3 add listeners before and after.
$(document).ready(listener); // goes off first.
$(document).ready(otherListener); // goes off 2nd.
And so on, it's 2 not hard to sandwich functions.
There's 1 no need to hack .ready()
imo.
I just had the exact same problem. I put 3 a tiny script on GitHub that adds a $.beforeReady() function. Funny 2 :)
https://github.com/aim12340/jQuery-Before-Ready
Just load the script right after jQuery 1 like in this example:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="jquery-before-ready.js"></script>
</head>
<body>
<script>
$(document).ready({
alert("This function is declared first!")
})
</script>
<script>
$.beforeReady(function(){
alert("This function is declared second but executes first!")
})
</script>
</body>
</html>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.