-
-
Save gf3/453438 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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(); | |
}); |
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
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)