Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* jQuery whenLoaded - v0.1pre - 06/25/2010
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
'$:nomunge' // Used by YUI compressor.
$.fn.whenLoaded = function( fn ) {
var selector = this.selector
if ( $.isFunction( fn ) )
$(function() { $( selector ) )
return this
// Usage:
Copy link

cowboy commented Jun 26, 2010

The whole point is not to defer the callback until DOM ready, but to execute it the instant the DOM element in question comes into existence (which, on a slow-loading page with lots of blocking scripts, could be noticeably earlier than DOM ready)

Copy link

gf3 commented Jun 28, 2010

But then it's only useful for straight ID queries. And even then, if it's a slow loading page and the element is near the end, you're just spamming the DOM with queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment