{"id":108,"date":"2007-08-14T13:59:21","date_gmt":"2007-08-14T00:59:21","guid":{"rendered":"http:\/\/www.thunderguy.com\/semicolon\/2007\/08\/14\/elementready-jquery-plugin\/"},"modified":"2015-03-31T15:40:14","modified_gmt":"2015-03-31T02:40:14","slug":"elementready-jquery-plugin","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2007\/08\/14\/elementready-jquery-plugin\/","title":{"rendered":"elementReady: a jQuery plugin"},"content":{"rendered":"<p><em>Please see <a href=\"http:\/\/thunderguy.com\/semicolon\/2013\/04\/02\/jquery-waitfor-plugin\/\"><code>waitFor<\/code><\/a>, the updated version of <code>elementReady<\/code>.<\/em><\/p>\n<p>I have written a simple but useful jQuery plugin. <code>elementReady<\/code> calls a function during page load as soon as a specific element is available &#8212; even before the full DOM is loaded. It&#8217;s useful if you have unobtrusive JavaScript that you want to apply to particular page elements immediately, without having to wait for the whole DOM to load in a large page.<\/p>\n<p>Using the elementReady plugin is very similar to the existing <code>jQuery.ready()<\/code> function, except that you must also pass in a string representing the ID of the element that should have the function call attached.<\/p>\n<p>This version of the plugin works well for me in testing (and on this page), but it has not been exhaustively tested. I would appreciate your comments on the idea and the implementation.<\/p>\n<h2>Download<\/h2>\n<p>Get <a href=\"https:\/\/raw.github.com\/bennettmcelwee\/jQuery.elementReady\/release\/jquery.elementReady.js\">jquery.elementReady.js<\/a> from the <a href=\"https:\/\/github.com\/bennettmcelwee\/jQuery.elementReady\">Github project<\/a>.<\/p>\n<p><del datetime=\"2012-10-16T02:00:04+00:00\">Get the latest version from the <a href=\"http:\/\/plugins.jquery.com\/project\/elementReady\">elementReady jQuery plugin page<\/a>.<\/del> <del datetime=\"2011-08-03T00:57:58+00:00\">Get <a href=\"http:\/\/thunderguy.com\/semicolon\/plaintext\/jquery.elementReady.js\">jquery.elementReady.js<\/a> here<\/del>. Then read on for instructions and details.<\/p>\n<h2>Documentation for jQuery.elementReady()<\/h2>\n<p><!-- Generated by http:\/\/jquery.bassistance.de\/docTool\/docTool.html\n--><span class=\"fn\">$.elementReady( <span class=\"arg-type tooltip\" title=\"A string of characters.\">String<\/span> <span class=\"arg-name tooltip\" title=\"string ID of the element to wait for\">id<\/span>, <span class=\"arg-type tooltip\" title=\"A reference to a Javascript function.\">Function<\/span> <span class=\"arg-name tooltip\" title=\"function to call when the element is ready\">fn<\/span> ) returns <span class=\"type\"><span class=\"tooltip\" title=\"A jQuery object.\">jQuery<\/span><\/span><\/span><\/p>\n<div class=\"more\">\n<div class=\"desc\">While a page is loading, call a given callback function as soon as a specific<br \/>\nelement is loaded into the DOM, even before the full DOM has been loaded.<br \/>\nExecutes the function within the context of the element. This means that when<br \/>\nthe passed-in function is executed, the &#8216;this&#8217; keyword points to the specific<br \/>\nDOM element.<\/p>\n<p>The function returns &#8216;this&#8217;, so you can chain multiple calls to<br \/>\nelementReady(). (Not that there&#8217;s much benefit in doing that.)<\/p>\n<p>One argument is passed to the callback: a reference to the jQuery function.<br \/>\nYou can name this argument $ and therefore use the $ alias even in<br \/>\nnoConflict mode.<\/p>\n<p>If the element has not been found by the time the DOM is fully loaded, then<br \/>\nthe function will not be called.<\/p><\/div>\n<div class=\"example\">\n<h3>Example:<\/h3>\n<p>Change the source of a specific image as soon as it is loaded into the<br \/>\nDOM (before the whole DOM is loaded).<\/p>\n<pre>$.elementReady('powerpic', function(){\r\n    this.src = 'powered-by-jquery.png';\r\n});<\/pre>\n<\/div>\n<div class=\"example\">\n<h3>Example:<\/h3>\n<p>If you want to have the jQuery object instead of the regular DOM<br \/>\nelement, use the $(this) function.<\/p>\n<pre>$.elementReady('header', function(){\r\n    $(this).addClass('fancy');\r\n});<\/pre>\n<\/div>\n<div class=\"example\">\n<h3>Example:<\/h3>\n<p>Chain multiple calls to $.elementReady().<\/p>\n<pre>$.elementReady('first',  function(){ $(this).fancify(); })\r\n .elementReady('second', function(){ $(this).fancify(); });<\/pre>\n<\/div>\n<div class=\"example\">\n<h3>Example:<\/h3>\n<p>Use the &#8216;$&#8217; alias within your callback, even in noConflict mode.<\/p>\n<pre>jQuery.noConflict();\r\njQuery.elementReady('header', function($){\r\n    $(this).addClass('fancy');\r\n});<\/pre>\n<\/div>\n<div class=\"example\">\n<h3>Example:<\/h3>\n<p>Change the polling interval to 100ms. This only works if <code>$.elementReady()<\/code> has not yet been called.<\/p>\n<pre>$.elementReady.interval_ms = 100;<\/pre>\n<\/div>\n<\/div>\n<h2>Why?<\/h2>\n<p>I <a href=\"http:\/\/groups.google.com\/group\/jquery-dev\/browse_thread\/thread\/263b3ee5c6e4e63e\/896356fc3f78c65d\">asked about this feature on the jQuery development list<\/a>, and to my surprise nobody had made much of an effort to do this. jQuery author John Resig has some <a href=\"http:\/\/groups.google.com\/group\/jquery-dev\/browse_thread\/thread\/6843a97e9e12df5c\/8629dd8bb941fec1\">magic code that makes this happen automatically<\/a>(!), but including this &#8220;auto&#8221; feature in the core would complicate things unnecessarily. He suggested that a plugin should be written. So here it is.<\/p>\n<h2>Implementation notes<\/h2>\n<p>The jQuery.elementReady() function is interesting because it&#8217;s quite unlike most other jQuery methods. It&#8217;s not a method applied to a normal jQuery object; it can&#8217;t be, because a jQuery object is normally constructed from a DOM element (or list of DOM elements). But in this case, all we have is the ID of an element: the element itself does not yet exist.<\/p>\n<p>When the function is called, the matched element is passed to the callback function as a raw DOM element rather than a jQuery object. This is consistent with other event handlers, and is also the most efficient approach.<\/p>\n<p>The function returns the global jQuery object, so you can chain calls to <code>elementReady()<\/code>.<\/p>\n<p>My implementation is inspired by the Yahoo UI library&#8217;s <a href=\"http:\/\/developer.yahoo.com\/yui\/docs\/YAHOO.util.Event.html#onAvailable\">onAvailable()<\/a>, but is simpler (and, to be sure, less well-tested). YUI also includes a related method called <a href=\"http:\/\/developer.yahoo.com\/yui\/docs\/YAHOO.util.Event.html#onContentReady\">onContentReady()<\/a>, which is supposed to fire when the element <em>and<\/em> all its content is available. I have not implemented that feature yet, but I think I may end up doing so in elementReady(). This will make it more robust yet still dead simple to use.<\/p>\n<p>I thought about somehow overloading the original <code>ready()<\/code> function but thought that would just add complexity for no real benefit.<\/p>\n<h2>Things to do<\/h2>\n<p>This is an early version of the plugin. I&#8217;m afraid I have tested it only on Windows browsers, but I have had no problems with it yet. The main thing to do is to test it more thoroughly. As I mentioned above, I also might decide to modify the function to check that the entire contents of the target element are loaded, not just the element itself.<\/p>\n<h2>Have your say<\/h2>\n<p>Apparently blog posts get x% more comments if the last heading says &#8220;Have your say&#8221;. So have <em>your<\/em> say! Is this useful? How can it be improved?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Please see waitFor, the updated version of elementReady. I have written a simple but useful jQuery plugin. elementReady calls a function during page load as soon as a specific element is available &#8212; even before the full DOM is loaded. It&#8217;s useful if you have unobtrusive JavaScript that you want to apply to particular page [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[21,40,18],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-general","tag-javascript","tag-jquery","tag-web-development"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":7,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":711,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/108\/revisions\/711"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}