News for hackers. Stuff that matters.
Re:Don’t let jQuery’s $(document).ready() slow you down by peregrine on Thursday August 19, 2010 @09:10AM (#1617228)
I'm not sure he knows how .live works. .live doesn't wait for items to be ready and it only binds one event on to the top level document. .live waits for bubbled events and if the item bubbling the event matches this .live selector it calls back. Pretty smart if you think about it.

But either way he makes a great point on the document.ready if your page depends on ajax or some sort of ui styling its probably best in the header script.

Re:Don’t let jQuery’s $(document).ready() slow you down by Encosia on Thursday August 19, 2010 @09:34AM (#1617305)
Right, that's the crux of my overall point about .live(). Since it's using event delegation, the DOM elements don't need to be present at the time you declare it. If you have the opportunity to set up your .live() handlers early, you should.
Re:Don’t let jQuery’s $(document).ready() slow you down by jakevoytko on Thursday August 19, 2010 @08:23AM (#1617141)
Important takeaway: Use Firebug's network monitoring to find out what actually happens when your page loads.
Re:Don’t let jQuery’s $(document).ready() slow you down by dododo on Thursday August 19, 2010 @08:34AM (#1617158)
the rest seems to be summarised as: don't wait for things you don't need to wait for.

$(document).ready() can be a bit of a sledgehammer in terms of latency.

Re:Don’t let jQuery’s $(document).ready() slow you down by pak on Thursday August 19, 2010 @10:18PM (#1619301)
Don't just base your conclusions on one page load, though, as this article seems to do. I wish there was a tool for Web Inspector/Firebug to select various elements over a series of page loads and give me statistics after e.g. 100 refreshes. Page load is highly variable and dependent on things like caching server-side, client-side, within the database, etc. as well as network conditions so repeating the tests is important.
Re:Don’t let jQuery’s $(document).ready() slow you down by Encosia on Friday August 20, 2010 @12:43AM (#1619531)
None of those conclusions are based on the two screenshots shown in the article. I just set those up and included them for illustrative purposes. The article's points themselves are based on my experience routinely using those optimizations in production.

Sorry if that wasn't very clear in the article.

Re:Don’t let jQuery’s $(document).ready() slow you down by vasi on Thursday August 19, 2010 @07:07PM (#1618970)
Seems to me that AJAX before document-ready could get complicated once you want to actually do something with the response. Normally one would do something like this:

  $(document).ready(function(){
    $.getJSON(my_url, function(data) {
      $('<p>').text(data.some_text).appendTo('#some_container);
    });
  });
But if we call getJSON before document-ready, the node $('#some_container') may not yet exist! Now we need some way to wait until both the AJAX request is complete and the document is ready…recommendations?

EDIT: Thanks for the responses, glad to know it's safer than I thought!

Re:Don’t let jQuery’s $(document).ready() slow you down by Encosia on Thursday August 19, 2010 @07:26PM (#1619018)
The way browsers implement both JavaScript and document rendering via a single-threaded execution queue, that isn't an issue. Once the page begins rendering, callbacks won't have a chance to execute until after the document is rendered and ready. That's the same mechanism that makes the setTimeout(fn, 0) trick work, for example.

It feels a bit like driving fast with no seat belt on, but it's safe.

If you do want to be extra cautious, MBlume's suggestion to wrap the success handler in $(document).ready() works fine too. That's effectively the same as not wrapping it.

Re:Don’t let jQuery’s $(document).ready() slow you down by MBlume on Thursday August 19, 2010 @07:12PM (#1618992)
swap the first two lines:

  $.getJSON(my_url, function(data) {
    $(document).ready(function(){
      $('<p>').text(data.some_text).appendTo('#some_container);
    });
  });
Re:Don’t let jQuery’s $(document).ready() slow you down by photon_off on Thursday August 19, 2010 @07:19PM (#1619012)
Very clever. This works because $(document).ready will execute immediately if the page is already loaded.
Re:Don’t let jQuery’s $(document).ready() slow you down by nphase on Thursday August 19, 2010 @10:08AM (#1617396)
I know this is off topic, but there's another reason it slows you down - too many keystrokes.

I prefer

$(function(){});

to

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

Re:Don’t let jQuery’s $(document).ready() slow you down by Encosia on Thursday August 19, 2010 @10:39AM (#1617469)
I'm torn when it comes to that. The readability of $(document).ready() is better. Especially for later devs who may not be familiar with jQuery.
Re:Don’t let jQuery’s $(document).ready() slow you down by billybob on Thursday August 19, 2010 @10:48AM (#1617493)
For devs not familiar with jQuery (and on the off-chance that you'll need no-conflicts mode at some point), you might even avoid the dollar sign.

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

Re:Don’t let jQuery’s $(document).ready() slow you down by JustinSeriously on Thursday August 19, 2010 @01:28PM (#1617949)
On the other hand, if a dev is that unfamiliar with jQuery code, they shouldn't maintaining jQuery code.

Using $(function(){}); serves as a nice warning to this effect.

Re:Don’t let jQuery’s $(document).ready() slow you down by rue on Thursday August 19, 2010 @08:05PM (#1619077)
That is an awful argument. Can you imagine what software would look like if people took that approach with everything? Development would have ground to a halt decades ago.
Re:Don’t let jQuery’s $(document).ready() slow you down by ganjianwei on Friday October 01, 2010 @01:27PM (#1747785)
I think the point is that they should learn enough jQuery before maintaining jQuery code.
Re:Don’t let jQuery’s $(document).ready() slow you down by IgorPartola on Thursday August 19, 2010 @11:29AM (#1617627)
Or even better:

  jQuery(document).ready(function($){});
Re:Don’t let jQuery’s $(document).ready() slow you down by Encosia on Thursday August 19, 2010 @11:54AM (#1617697)
You can also use the module pattern[1] to isolate code from tampering or conflicts:

  (function($, window, undefined) {
    $(document).ready({
      // etc.
    });
  }(jQuery, window);
[1] http://www.yuiblog.com/blog/2007/06/12/module-pattern/
Re:Don’t let jQuery’s $(document).ready() slow you down by chc on Thursday August 19, 2010 @02:54PM (#1618199)
Even further off-topic, I really like the succinctness of the former in CoffeeScript:

  $(function () {
      $('#index').append($('article p:first-child'));
  });
becomes:

  $ -> $('#index').append $('article p:first-child')
Not exactly noob-friendly, but very concise.
Re:Don’t let jQuery’s $(document).ready() slow you down by Smuggler on Thursday August 19, 2010 @05:04PM (#1618654)
This is my preferred pattern:

jQuery(function($){

});

It would avoid noConflict type problems.

Re:Don’t let jQuery’s $(document).ready() slow you down by tszming on Thursday August 19, 2010 @11:43AM (#1617656)
.live() needs more promotion.
Re:Don’t let jQuery’s $(document).ready() slow you down by paulirish on Thursday August 19, 2010 @02:29PM (#1618120)
If you like live(), you'll enjoy delegate() more.

But, regardless, event delegation needs more love, yup.