Skip to content Skip to sidebar Skip to footer

Jquery Ready, Is It Always Needed?

Regarding my question here: Validate dynamically added control Should we always use the ready function on javascripts?

Solution 1:

JQuery documentation shows this below:

All three of the following syntaxes are equivalent:

• $(document).ready(handler)

• $().ready(handler) (this is not recommended)

• $(handler)

Any JQuery starting with $(handler) executes when the DOM is ready. You don't need to worry about the ready bit.

SOURCE: http://api.jquery.com/ready/

Solution 2:

No: it is only needed if you need to wait for the DOM to finish loading.

You can also use the "live" function to bind which may decrease page load times as described here http://encosia.com/2010/08/18/dont-let-jquerys-document-ready-slow-you-down/

Solution 3:

Read this

The .ready() method is generally incompatible with the attribute. If load must be used, either do not use .ready() or use jQuery's .load() method to attach load event handlers to the window or to more specific items, like images.

U can Google or search on SO for similar answer

Solution 4:

You do not need to use document ready. place your jQuery just before </body> tag and your other jQuery scripts and plugins after your jQuery as such.

<scriptsrc="jquery.js"></script><scriptsrc="other.js"></script><script>
  $('div').click.....
</script></body>

There's a great article about this "don't let document ready slow you down"

Solution 5:

Depends the element your workin on, you have many possibilities. If it's juste for a navigationbar or a module you can chose different DOM elements.

/* for a menu */
$('.menu').ready(function() {...})

/* for the all page */
$(docuement).ready(function() {...})

But you can also include your jquery file and your script at the end of page.

<scriptsrc="jquery.js"></script><script>
  $('.menu').click(function() {...})
</script>

Post a Comment for "Jquery Ready, Is It Always Needed?"