Johan
Johan

Reputation: 35194

Using event handlers on document before the DOM is loaded

Would it be ok to add an ajaxStop() event handler to the document before the DOM is loaded? It isn't related to any DOM elements, so as far as I can tell it shouldn't matter. Am I right?

$(document).ajaxStop(function () {
    console.log('ajax complete');     
});

vs

$(function(){

    $(document).ajaxStop(function () {
        console.log('ajax complete');     
    });

});

Upvotes: 0

Views: 50

Answers (1)

gen_Eric
gen_Eric

Reputation: 227190

$(function(){}) is actually shorthand for $(document).ready(function(){}).

So, by using $(function(){}) you already are binding an event to document before the DOM is ready.

By that logic, it's fine to bind ajaxStop to document before the DOM is ready.

Upvotes: 1

Related Questions