I am using RequireJS and need to initialize something on DOM ready. Now, RequireJS provides the domReady
plugin, but we already have jQuery's $(document).ready()
, which is available to me since I have required jQuery.
So I have got two options:
Use the
domReady
plugin:require(['domReady'], function (domReady) { domReady(function () { // Do my stuff here... }); });
Use
$(document).ready()
:$(document).ready(function() { // Do my stuff here... });
Which one should I choose, and why?
Both the options seems to work as expected. I am not confident in the jQuery's one because RequireJS is doing its magic; that is, since RequireJS will dynamically add scripts, I'm worried that DOM ready may occur before all of the dynamically-requested scripts are loaded. Whereas, RequireJS will add a burden on additional JS just for domReady
when I already have jQuery required.
Questions
- Why does RequireJS provide a
domReady
plugin when we can have jQuery's$(document).ready();
? I don't see any advantage of including another dependency. - If its just to feed a need, then why not provide one for cross-browser AJAX?
As far as I know, a module that requires domReady
won't be fetched or executed after the document is ready, and you could do the same requiring jQuery as well:
require(['jQuery'], function ($) {
$(document).ready(function () {
// Do my stuff here...
});
});
To be more clear on my question: what's the difference between requiring domReady
or jQuery
?
I am not confident in jquery's dom ready
i want to mark it as offensive:p
script
tags go, or are you writing a library/plug-in that other people will use (and so they're in control of the location of thescript
tags in the markup)?I am not confident in jquery's dom ready because requirejs is doing its magic.
Since, require is encapsulating jquery in limited local scope. That is not the point. (as far as the question is concerned).