Need .ready () in jQuery

I have seen various examples use this, and I am curious to know if it is dangerous not to turn jQuery code into the next?

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

I know what he is doing, and I know why you are doing it, but I am curious if it is more dangerous or just a bad practice / style to not have it? Thank!

+3
source share
5 answers

You use it if your code needs to access the DOM.

If you simply configure classes and modules, but do not actually execute them, you do not need to wrap them in a ready-made handler.

However, if you are doing something that requires loading elements (for example, adding event handlers), you need to do this in the ready () event.

EDIT:

: http://jsfiddle.net/ctrlfrk/43n8U/ addHandler , .

( , jsfiddle head, onload, )

+6

, . script <body>, , DOM . , script <head> DOM, script , script hasn ' t DOM.

+4
+1

ready() . "" - .

But many times you want to run some code that will request the DOM created after the script -tag in which your code is located, or you just need some structures from the document that are placed after this script tag. If so, then ready () for you.

But as a rule, there is no good reason not to use the ready () function, so every baud almost always uses it.

+1
source

I think you should also know jQuery $(window).load()and how it differs from$(document).ready()

Read more about it here.

0
source

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


All Articles