Modernizr vs $ (document) .ready ()

It appears on the network panel, when you download scripts via Modernizr.load, it does not actually block $ (document) .ready ().

So, theoretically, let's say that I add json2.js through Modernizr, and another developer is trying to use JSON.parse in $ (document) .ready (), their script can really break due to race conditions.

Is it possible to fix this without trying to polish all the code checks?

+6
source share
1 answer

I would think so: using Modernizr (and browser tests) to load Javascript files is a very common design decision that everyone in your team should know about anyway.

However, you probably want to implement (if you still do not know) some kind of initialization of your application, which should be the last thing you need to start - after everything has been loaded. This initialization can be done in the yepnope callback, apparently even in the shell of $(document).ready() .

Yepnope claims in this context in "common mistakes":

Just because your script is executed does not mean the document is ready. Remember that you can use document callbacks inside your yepnope callbacks . If you play with the DOM, we strongly ask you to do this because your test environment can act than your production server, the speed is significantly different.


And for the sake of completeness:

Without checking them further, here are a couple of other ideas on how to deal with document.ready + Modernizr.load : How to use yepnope.js with $ (document) .ready () effectively? and fooobar.com/questions/904883 / ...

+4
source

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


All Articles