TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Don’t let jQuery’s $(document).ready() slow you down

139 pointsby gspyroualmost 15 years ago

5 comments

peregrinealmost 15 years ago
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.<p>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.
评论 #1617305 未加载
jakevoytkoalmost 15 years ago
Important takeaway: Use Firebug's network monitoring to find out what actually happens when your page loads.
评论 #1617158 未加载
评论 #1619301 未加载
vasialmost 15 years ago
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:<p><pre><code> $(document).ready(function(){ $.getJSON(my_url, function(data) { $('&#60;p&#62;').text(data.some_text).appendTo('#some_container); }); }); </code></pre> But if we call getJSON before document-ready, the node $('#some_container') may not yet exist! Now we need some way to wait until <i>both</i> the AJAX request is complete <i>and</i> the document is ready…recommendations?<p>EDIT: Thanks for the responses, glad to know it's safer than I thought!
评论 #1619018 未加载
评论 #1618992 未加载
评论 #1618997 未加载
nphasealmost 15 years ago
I know this is off topic, but there's another reason it slows you down - too many keystrokes.<p>I prefer<p>$(function(){});<p>to<p>$(document).ready(function(){});
评论 #1617469 未加载
评论 #1618199 未加载
评论 #1618654 未加载
tszmingalmost 15 years ago
.live() needs more promotion.
评论 #1618120 未加载