When should you not use $ (document) .ready (function () for event handlers?

I'm new to jquery and javascript and just want to know why don't you want to use a function .ready()for all your event handlers?

Could there be potential problems if the user sends an input to a mouse event or keyboard event before the entire page is displayed?

+5
source share
5 answers

It should never be, the jQuery document is ready to go when the DOM is loaded. It does not wait for the full page to load (including images, etc.). It would be extremely rare for the user to respond in a timely manner to an attempt to run something before executing your code. Read this: http://api.jquery.com/ready/

In particular, the first paragraph:

JavaScript , , , , . script , DOM . , .ready(), , DOM , , , jQuery.

, $(document).ready(function() {}) $(function() {}) .

EDIT: , , , . , jQuery CDN. CDNs , - , CDN, .

+3

, $(document).ready(). , HTML DOM , , - , .

, , , .

+1

, , - - , ajax. - .

+1

.ready().

$(document).ready(function(){
  //all event handlers here
});

, , ?

, . , jQuery , , jQuery. , ; , .

0

, , ? [ ] script .ready()

, JavaScript, , .. DOM. , ( jQuery), CSS - .

.ready() , , , "" JS , , DOM. , , .

, JavaScript ( ) .ready() , script, , (), , , html. :

<input type="text" id="input1">
<script>
   // following will work, because "input1" already exists
   $("#input1").change(function() { /* do something */ });

   // following will NOT work, because "input2" has not been parsed yet
   $("#input2").change(function() { /* do something */ });
</script>
<input type="text" id="input2">

, - - , , .ready() - .

, .ready(), script , ( .ready()) .

.ready() JS , , . <head>, , , .ready().

0

All Articles