Will $(document ).ready() fire if the code which checks is inserted after the event happend?

时间:2016-04-15 15:15:37

标签: javascript jquery event-listener document-ready

I have a page where some additional Javascript code is downloaded to the page after the page itself has been loaded, (for example when the user clicks a button, a call is made to the server which pulls down an extra .js file).

If that additional code has a check for DOM ready like so..

  $( document ).ready(function() {
   console.log( "will they see me?" );
 });

Will that line be printed to the console since by the time this code runs on the page, the jQuery DOM ready event has already fired?

In other words, does this check need to be present when the actual event happens or would it also fire after the event happened?

1 个答案:

答案 0 :(得分:2)

Yes. You've got a method to test that in your question already:

$(document).ready(function() {
   console.log("will they see me?");
});

Run that in the console, and watch the message get printed out.