[ACCEPTED]-Running a function just before $(document).ready() triggers-document-ready

Accepted answer
Score: 16

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...


$(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() {
Score: 0

$(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.

Score: 0

I just had the exact same problem. I put 3 a tiny script on GitHub that adds a $.beforeReady() function. Funny 2 :)


Just load the script right after jQuery 1 like in this example:

        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="jquery-before-ready.js"></script>
                alert("This function is declared first!")
                alert("This function is declared second but executes first!")

More Related questions