Skip to content

Instantly share code, notes, and snippets.

@gf3
Forked from cowboy/jquery.ba-ready.js
Created June 25, 2010 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gf3/453438 to your computer and use it in GitHub Desktop.
Save gf3/453438 to your computer and use it in GitHub Desktop.
/*!
* jQuery whenLoaded - v0.1pre - 06/25/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// NOTE: TOTALLY UNTESTED!
(function($){
'$:nomunge' // Used by YUI compressor.
$.fn.whenLoaded = function( fn ) {
var selector = this.selector
if ( $.isFunction( fn ) )
$(function() {
fn.call( $( selector ) )
})
return this
}
})(jQuery)
// Usage:
$('#someid').whenLoaded(function(){
this.doSomething();
});
@cowboy
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)

@gf3
Copy link
Author

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