Don’t let jQuery’s $(document).ready() slow you down
11–20 of 25 posts
Re: Don’t let jQuery’s $(document).ready() slow you down
#12Earlier quoted context omitted.
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(){});
Or even better: jQuery(document).ready(function($){});
(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
#13Earlier quoted context omitted.
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.
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(){});
Using $(function(){}); serves as a nice warning to this effect.
Re: Don’t let jQuery’s $(document).ready() slow you down
#14.live() needs more promotion.
But, regardless, event delegation needs more love, yup.
Re: Don’t let jQuery’s $(document).ready() slow you down
#15I know this is off topic, but there's another reason it slows you down - too many keystrokes. I prefer $(function(){}); to $(document).ready(function(){});
$(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
#16I know this is off topic, but there's another reason it slows you down - too many keystrokes. I prefer $(function(){}); to $(document).ready(function(){});
jQuery(function($){
});
It would avoid noConflict type problems.
Re: Don’t let jQuery’s $(document).ready() slow you down
#17 $(document).ready(function(){
$.getJSON(my_url, function(data) {
$('').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
#18Seems 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) { $(' ').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…
$.getJSON(my_url, function(data) {
$(document).ready(function(){
$('').text(data.some_text).appendTo('#some_container);
});
});
Re: Don’t let jQuery’s $(document).ready() slow you down
#19Seems 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) { $(' ').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…
Re: Don’t let jQuery’s $(document).ready() slow you down
#20Seems 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) { $(' ').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…
swap the first two lines: $.getJSON(my_url, function(data) { $(document).ready(function(){ $(' ').text(data.some_text).appendTo('#some_container); }); });