Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From: <a href="http://web.archive.org/web/20120904060007/http://lucassmith.name/2008/11/is-my-image-loaded.html" rel="nofollow noreferrer">http://lucassmith.name/2008/11/is-my-image-loaded.html</a></p> <pre><code>// First a couple helper functions function $(id) { return !id || id.nodeType === 1 ? id : document.getElementById(id); } function isType(o,t) { return (typeof o).indexOf(t.charAt(0).toLowerCase()) === 0;} // Here's the meat and potatoes function image(src,cfg) { var img, prop, target; cfg = cfg || (isType(src,'o') ? src : {}); img = $(src); if (img) { src = cfg.src || img.src; } else { img = document.createElement('img'); src = src || cfg.src; } if (!src) { return null; } prop = isType(img.naturalWidth,'u') ? 'width' : 'naturalWidth'; img.alt = cfg.alt || img.alt; // Add the image and insert if requested (must be on DOM to load or // pull from cache) img.src = src; target = $(cfg.target); if (target) { target.insertBefore(img, $(cfg.insertBefore) || null); } // Loaded? if (img.complete) { if (img[prop]) { if (isType(cfg.success,'f')) { cfg.success.call(img); } } else { if (isType(cfg.failure,'f')) { cfg.failure.call(img); } } } else { if (isType(cfg.success,'f')) { img.onload = cfg.success; } if (isType(cfg.failure,'f')) { img.onerror = cfg.failure; } } return img; } </code></pre> <p>And here how to use it:</p> <pre><code>image('imgId',{ success : function () { alert(this.width); }, failure : function () { alert('Damn your eyes!'); }, }); image('http://somedomain.com/image/typooed_url.jpg', { success : function () {...}, failure : function () {...}, target : 'myContainerId', insertBefore : 'someChildOfmyContainerId' }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload