Can jQuery ready () function be used twice for the same element?

I want to use jQuery ready() function for a document element. Here are my scripts:

First page:

 $(document).ready(function(){ $('form#haberekle').ajaxForm(options); }); 

Second page:

  $(document).ready(function() { var options = { success:showResponse, beforeSubmit:showRequest, resetForm:true }; $('#haberresmiekleform').ajaxForm(options); }); 

These two pages are included on the same main page with <!--#include file=""-->

Can these two functions work properly or block each other? In my experience, they seem to be correct.

For example: the onclick function of a button is only one.

+6
source share
4 answers

You can have as many .ready() as you want, jQuery is designed with that in mind, and this is completely normal.

So yes, this is normal and you will not have any problems ... this happens all the time.

Think of it as an event handler, for example .click() , this is exactly how it behaves (well, strictly speaking, not quite like that, but for most purposes like this). That way you can have as much as you want.

Another note that may be of interest is that the handlers you pass to .ready() are transferred via .done() to readyList , which means that they will execute in the order that you named them on the page. The same order behavior is true (albeit via an array, another method) in earlier versions of jQuery.

+10
source

Yes, it is normal. Both will be called when dom is fully loaded. Note that if you call .ready() after the dom is already loaded, the callback will be executed immediately. See http://api.jquery.com/ready/

+2
source
+2
source

There is no problem attaching multiple handlers for the same event. The documentation for ready states:

There is also $ (document) .bind ("done", handler). This behaves like a ready-made one, with one exception: if the ready-made event is already running, and you try .bind ("ready"), the associated handler will not be executed.

And the documentation for bind says:

When an event reaches an element, all handlers associated with this type of event for the element are fired. If there are several registered handlers, they will always be executed in the order that they were connected.

+1
source

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


All Articles