What is the difference between $ (document) .bind ('ready', function) and $ (document) .ready (function () {})

I want to upgrade from requirejs version 2.0.0 to 2.1.5

Here is the code:

define(['jquery', 'test.js'],
    function ($, test) {
    var test = new $.test({
        //options
    });
    ....
});

test.js

(function($) {
    var registerEvents = function() {
        //dosth
    };
    $.test = function(options) {
        $(document).bind('ready', function() {
            registerEvents();
        });
        ...
        return test;
    }

    ...
});

In version 2.0.0, requirejs stores the dom ready event until all resources are loaded, so it worked correctly https://github.com/jrburke/requirejs/issues/249

When I upgrade to requirejs version 2.1.5, the registerEvents function will never be called.

But, surprisingly, if I change:

$(document).bind('ready', function() {
    registerEvents();
});

To:

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

It worked great

So my question is: what is the difference between the two?

Edit: I am using jQuery v1.7.2

$ (document) .on ('ready', function () {}) does not work

+4
source share
1 answer

,

$(document).on( "ready", ), jQuery 1.8. , , , .on( "ready" ) . , . [em mine]

.bind .on .


$( document ).ready( handler )
$().ready( handler ) // (this is not recommended)
$( handler )

$( document ).on( "ready", handler )
$( document ).bind( "ready", handler )

, , .

+9

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


All Articles