Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requirejs domReady plugin vs Jquery $(document).ready()?

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:

  1. Use the domReady plugin:

    require(['domReady'], function (domReady) {     domReady(function () {         // Do my stuff here...     }); }); 
  2. 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?

like image 368
Yugal Jindle Avatar asked Mar 11 '13 06:03

Yugal Jindle


People also ask

What is difference between $( function () and document Ready?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.

What is the purpose of $( document ready ()?

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 jQuery document ready?

jQuery ready() MethodThe ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

What is the benefit of using document ready in jQuery?

jQuery document ready is used to initialize jQuery/JavaScript code after the DOM is ready, and is used most times when working with jQuery. The Javascript/jQuery code inside the $(document). ready() function will load after the DOM is loaded, yet before the page contents load.


2 Answers

It seems like all the key points were already hit, but a few details fell through the cracks. Mainly:

domReady

It is both a plugin and a module. If you include it in the the requirements array w/ a trailing ! your module won't execute until it's "safe" to interact w/ the DOM:

define(['domReady!'], function () {     console.info('The DOM is ready before I happen'); }); 

Note that loading and executing are different; you want all your files to load as soon as possible, it's the execution of the contents that is time sensitive.

If you omit the !, then it's just a normal module that happens to be a function, which can take a callback that won't execute before the DOM is safe to interact with:

define(['domReady'], function (domReady) {     domReady(function () {         console.info('The DOM is ready before I happen');     });     console.info('The DOM might not be ready before I happen');         }); 

Advantage when using domReady as a plugin

Code that depends on a module that in turn depends on domReady! has a very significant advantage: It does not need to wait for the DOM to be ready!

Say that we have a block of code, A, that depends on a module, B, that depends on domReady!. Module B will not finish loading before the DOM is ready. In turn, A will not run before B has loaded.

If you were to use domReady as a regular module in B, it would also be necessary for A to depend on domReady, as well as wrapping its code inside a domReady() function call.

Furthermore, this means that domReady! enjoys that same advantage over $(document).ready().

Re the differences between domReady and $(document).ready()

Both sniff out whether/when the DOM is ready in essentially the same way.

Re fear of jQuery firing at the wrong time

jQuery will fire any ready callback even if the DOM loads before jQuery does (your code shouldn't care which happens first.).

like image 131
fncomp Avatar answered Sep 22 '22 08:09

fncomp


An attempt at answering your main question:

Why does requirejs provides a domReady plugin when we can have jquery's $(document).ready();?

They do two different things, really. RequireJS' domReady dependency signifies that this module requires the DOM to be completely loaded before it can be run (and can therefore be found in any number of modules in your application if you so desire), while $(document).ready() instead fires off its callback functions when the DOM is done loading.

The difference may seem subtle, but think of this: I have a module that needs to be coupled to the DOM in some way, so I can either depend on domReady and couple it at module definition time, or put down a $(document).ready() at the end of it with a callback to an init function for the module. I'd call the first approach cleaner.

Meanwhile, if I have an event that needs to happen right as the DOM is ready, the $(document).ready() event would be the go-to, since that does not in particular depend on RequireJS being done loading modules, provided the dependencies of the code you're calling it from are met.

It's also worth considering that you do not necessarily use RequireJS with jQuery. Any library module that needs DOM access (but does not rely on jQuery) would then still be useful by using domReady.

like image 20
Gert Sønderby Avatar answered Sep 24 '22 08:09

Gert Sønderby