Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do jQuery scripts still need $(document).ready if they are loaded after all of the page HTML?

If I'm loading my jQuery scripts below all of my page HTML, do I still need to wait for $(document).ready to be able to use jQuery to find elements in the page?

like image 248
Kevin Burke Avatar asked May 01 '12 02:05

Kevin Burke


People also ask

Is $( document .ready necessary?

No, it is not necessary.

Can I use jQuery without document ready?

It is in no way required. All it does is make sure that the DOM is loaded before trying to query for and use the elements inside the DOM (usually after dom is ready and before body. onload fires). You can use jquery perfectly fine without it.

What does $( document .ready do?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is the purpose of this jQuery function $( document .ready function?

jQuery $(document). ready() is a basic part of using jQuery. The jQuery document ready function executes when the DOM (Document Object Model) is completely loaded in the browser. jQuery document ready is used to initialize jQuery/JavaScript code after the DOM is ready, and is used most times when working with jQuery.


1 Answers

No because the document would have already been loaded. The Dom loads top to bottom. I personally like to put all my js at the bottom of the page instead of in the head.

however it is only 1 line of code and i would suggest using it just to be safe. also you can make it even shorter. $(function() {} is the same as $(document).ready(function(){})

like image 187
Yamiko Avatar answered Sep 20 '22 15:09

Yamiko