Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why still using deprecated jQuery(document).ready()? #14620

Closed
alexantr opened this issue Aug 9, 2017 · 7 comments
Closed

Why still using deprecated jQuery(document).ready()? #14620

alexantr opened this issue Aug 9, 2017 · 7 comments
Assignees
Labels
JS JavaScript related
Milestone

Comments

@alexantr
Copy link
Contributor

alexantr commented Aug 9, 2017

yii\web\View uses not recommended way to include scripts in POS_READY section.

jQuery recommends to use

jQuery(function () { /* code */ });

instead

jQuery(document).ready(function () { /* code */ });

Also see in jQuery docs:

There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8 and removed in jQuery 3.0. Note that if the DOM becomes ready before this event is attached, the handler will not be executed.

One more thing. You can pass jQuery object to handler with $:

jQuery(function ($) {
    // using $ here will be safely even jQuery.noConflict() will be enabled
});

And one more thing. To use $ safely in POS_LOAD section construction jQuery(window).on('load', function () { }); can be placed into jQuery(function ($) { });:

jQuery(function ($) {
    $(window).on('load', function () {
        // POS_LOAD scripts. Can use $
    });
    // POS_READY scripts
});

Additional info

Q A
Yii version 2.0.*
PHP version 5.4-7.x
Operating system all
@samdark samdark self-assigned this Aug 9, 2017
@samdark samdark added the JS JavaScript related label Aug 9, 2017
@samdark samdark added this to the 2.0.14 milestone Aug 9, 2017
@klimov-paul
Copy link
Member

Relates #13839

@SilverFire SilverFire modified the milestones: 2.0.13, 2.0.14 Sep 9, 2017
@SilverFire
Copy link
Member

Related to #14338

@SilverFire
Copy link
Member

Resolved by 3a8feb1

@SilverFire SilverFire assigned SilverFire and unassigned samdark Sep 9, 2017
@klimov-paul
Copy link
Member

Complete removal of the JQuery.ready() looks pretty odd: this method still exists at jQuery library and I do not see any note about its been deprecated.
Would not its usage removal break particular JS code?

@alexantr
Copy link
Contributor Author

alexantr commented Sep 9, 2017

My mistake. Only .on("ready", handler) deprecated and removed in 3.0, not .ready(handler).
But anyway short syntax $(handler) is equivalent:

jQuery offers several ways to attach a function that will run when the DOM is ready. All of the following syntaxes are equivalent:
$( handler )
$( document ).ready( handler )
$( "document" ).ready( handler )
$( "img" ).ready( handler )
$().ready( handler )

@gpcaretti
Copy link

@alexantr

My mistake. Only .on("ready", handler) deprecated and removed in 3.0,
not .ready(handler). But anyway short syntax $(handler) is equivalent:
$( handler )
$( document ).ready( handler )

This is not correct. The jquery document continues (see here):
"As of jQuery 3.0, only the first syntax is recommended; the other syntaxes still work but are deprecated."

@Shashimii
Copy link

This issue helped me understanding jquery syntax 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS JavaScript related
Projects
None yet
Development

No branches or pull requests

6 participants