What is difference between $(document).ready(function(){ vs $(function(){ ?
Manoj Kalla
Select an image from your device to upload
What is difference between
$(document).ready(function(){
});
vs
$(function(){
$(document).ready() { }This uses the j-query library to detect when the DOM is ready for JavaScript to execute. Specifically, you are passing in the document object as an argument to the j-query constructor and calling the ready() function on it, which will wait until the ready event fires.
(function() { }) ()This is an anonymous function that instantaneously runs.The difference is that if you were to put these two lines next to each, whether both will run will depend on where the page state is. Once your code reaches the anonymous function it will run it regardless. Once it hits the j-query snippet, if your page is still loading dependencies, etc. and the ready event isn’t fired then your code in the curly braces will not run until it is.
$(function() { //some code});
$(function() {
//some code
Is a Shorthand version of:
$( document ).ready(function() { //some code});
$( document ).ready(function() {
So technically they are both the same.
$(function(){});is the shortest version of $(document).ready(function(){})