Differences between $ (document) .ready (function () {}) and $ (document) .on ('ready', function (e)

I see a lot of projects using

$(document).on('ready', function(e){
 //jquery stuff
})

Instead:

$( document ).ready(function(  ) {
  // Code using $ as usual goes here.
});

or

 $(function() {
  // Handler for .ready() called.
 });

I read the full api documentation , but I don't see how this works in the first example.

For me, using onthe first example is useless.

What is the difference between the cases?

+4
source share
1 answer

Function:

$( document ).ready(function ( ) {
  // Code using $ as usual goes here.
});

Translated to:

$( document ).on( 'ready', function (e) {
 //jquery stuff
})

Same thing with these abbreviated features:

$( element ).click( function ( ) { } );
$( element ).hover( function ( ) { } );
$( element ).load( function ( ) { } );
$( element ).scroll( function ( ) { } );

From the documentation .click( handler(eventObject) ) :

This method is a shortcut for .on( "click", handler )the first two options, and .trigger( "click" )- in the third.


Updated Answer - The Difference!

$(document).on( "ready", handler ), jQuery 1.8. , , .on( "ready" ) . : .

-

# 2

jQuery(function(){});

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

, .

+8

All Articles