JQuery: Forced execution of calls to document.ready ()

I am working on a codebase with several blocks of code, setting some kind of behavior on document.ready () (jQuery). Is there a way to ensure that one particular block is called before any of the others?

Background: I need to detect JS errors in an automated test environment, so I need code that starts reporting JS errors for execution before any other JS code runs.

+23
source share
5 answers

document.ready() callbacks are called in the order in which they were registered. If you register a test callback first, it will be called first.

In addition, if your test code does not really need to manipulate the DOM, you can run it when the code is parsed, and do not wait until the DOM is ready, which will be executed before other document.ready() callbacks are called , Or, perhaps you can immediately run part of your test code and defer only the part using the DOM to document.ready() .

Another idea (for testing only) you can run with a slightly modified version of jQuery, which added the document.ready() flag, which, when passed and set to true indicated to call this function first, or you can add a new document.readyFirst() , which will call your function first. This will be due to minor changes to the document.ready() processing code in jQuery.

+22
source

Like @ jfriend00 to mention that ready callbacks are called in the order they were registered.

So, even if this code block is called at the beginning, you can set another callback in it so that it runs at the very end.

 jQuery(document).ready(function(){ jQuery(document).ready(function(){ // This block will be executed after all ready calls. }); }); 
+7
source

You can use holdReady for this. As long as it is turned on before your events, you can run your own code and then run other ready-made events.

+6
source

You can execute the initial logging code in DOMReady and other initialization at boot. Or you can create your own initialization manager, which first calls your logging source code, and then all the other initialization callbacks (provided that they are registered with your manager and not jQuery if you don't want to hack jQuery and extract them there , t advise).

0
source

I tried to do something like this, I loaded a bunch of polypoluses that had .ready () functions, but I also had my own .ready () s on the page that I needed to execute first, although the polypoluses were loaded in the header .

I found a good way to get around this with .ready () very early (right after loading jQuery), which executes functions from an array. Since it is very early in the source code, it starts .ready () first. Further down the page, I can add functions to the array and, thus, all of them are executed before any other .ready () s.

I wrapped it in a .beforeReady () function and put it on GitHub as jQuery-Before-Ready for anyone interested. https://github.com/aim12340/jQuery-Before-Ready

0
source

Source: https://habr.com/ru/post/1257503/


All Articles