Good cross-browser ready-made event without using jQuery?

I need to create an equivalent jQuery ready event without using jQuery. It should work as many browsers as possible and cannot ruin the body.onload handler (i.e. if the handler is already installed, the function should not overwrite it). I checked the jQuery code, but don’t understand how it works, because it uses many jQuery functions.

Any suggestion on how to do this?

Edit: I have no control over where my code will be inserted, so it should play as best as possible with the existing body.onload handler. It also means that I cannot be sure that the code will be inserted at the bottom of the page (most likely, this will not happen).

+6
source share
3 answers
+1
source

The smallest DOMReady cross-browser code ever.

 <html> <head> <script> var ready = function (f) { (/in/.test(document.readyState)) ? setTimeout('r(' + f + ')', 9) : f(); }; </script> </head> <body> <script> ready(function () { alert('DOM Ready!'); }); </script> </body> </html> 
+3
source

Just add the <script> to the very bottom. That way, it will only load after the rest of the content has finished loading.

0
source

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


All Articles