New course! Every coder should learn Generative AI!
Try a free lesson+ 1
What is the difference between 'function' and 'document. Ready()'?
Hello everyone I have a question in Javascript language : What is the difference between 'function' and 'document. Ready()' when we want use a method after full load page?
2 Answers
+ 1
The major difference is that document.ready() does not exist. You are probably referring to jquery(document).ready() method in the jQuery library.
If you want to execute a statement after the DOM is loaded use this
document.addEventListener('DOMContentLoaded', function(){
//statement goes here
});
+ 1
document.ready() initializes kind of like onload() and determines if the document is ready to interface with but is limited to jQuery if my memory serves me. A normal function sets the block and you would call it later on and is pretty standard. You could run a normal function without jQuery using vanilla JavaScript when the script is loaded too. The difference wouldn’t be terribly noticable unless you need execution to only trigger once the DOM is completely ready to interface with.