Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modernizr vs $(document).ready()

It appears from network panel, when you load scripts through Modernizr.load, it doesn't actually blocks $(document).ready().

So theoretically, let's say that I add json2.js through Modernizr, and another developer tries to use JSON.parse in $(document).ready(), their script could actually break because of the race condition.

Would it be possible to remedy this without try to police all the code check-ins?

like image 630
voidvector Avatar asked Jan 02 '12 07:01

voidvector


People also ask

What is modernizr used for?

Modernizr is a JavaScript library that detects which HTML5 and CSS3 features your visitor's browser supports. In detecting feature support, it allows developers to test for some of the new technologies and then provide fallbacks for browsers that do not support them.

What is $( document ready () function Why should you use it?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.

What is $( document ready () equivalent in JavaScript?

jQuery $(document). ready() Equivalent in JavaScript This event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.


1 Answers

I'd see it this way: Using Modernizr (and browser tests) to load Javascript files is a very general design decision everyone on your team needs to be aware of in any case.

However, you probably want to implement (if you haven't yet) some sort of initialization of your application, which should be the last thing to be run — after everything has been loaded. This initialization can then be executed in a yepnope callback, apparently even within a $(document).ready() wrapper.

Yepnope states in this context in the "common gotchas":

Just because your script is done, doesn't mean the document is ready. Don't forget that you can use document ready callbacks inside of your yepnope callbacks. If you're toying with the DOM, we'd heavily encourage you to do so, because your test environment may act differently than your production server the speeds are dramatically different.


And for the sake of completeness:

Without checking them any further, here a couple of other ideas on how to deal with document.ready + Modernizr.load: How can I use yepnope.js with $(document).ready() effectively? and https://stackoverflow.com/a/6767886/564721

like image 87
polarblau Avatar answered Nov 12 '22 08:11

polarblau