Similar to https://gist.github.com/987446 This version is smaller and provide load/error callback
Usage:
image('http://yandex.ru/favicon.ico', function(img){/* do stuff */});
Demo: http://jsfiddle.net/X9jAq/
@p01 thx
Similar to https://gist.github.com/987446 This version is smaller and provide load/error callback
Usage:
image('http://yandex.ru/favicon.ico', function(img){/* do stuff */});
Demo: http://jsfiddle.net/X9jAq/
@p01 thx
function(s,l){ | |
with(new Image) // my lovely ugly hack | |
onload=onerror=function(){ | |
l(this) // pass image to callback | |
}, | |
src=s // set src | |
} | |
// shorter one with `this` binding to Image instance in callback by @p01 | |
// function(s,l){with(new Image)onload=onerror=l,src=s} |
function(s,l){with(new Image)onload=onerror=function(){l(this)},src=s} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Mikhail Davydov <[email protected]> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "image", | |
"description": "Tiny image loader", | |
"keywords": [ | |
"image", | |
"preloader", | |
"loader" | |
] | |
} |
<!DOCTYPE html> | |
<title>Tiny image loader</title> | |
<div>Expected value: <b>width 16px</b></div> | |
<div>Actual value: <b id="ret">loading...</b></div> | |
<script> | |
var image = function(s,l){with(new Image)onload=onerror=function(){l(this)},src=s}; | |
image('http://yandex.ru/favicon.ico', function (img) { | |
document.getElementById( "ret" ).innerHTML = 'width ' + img.width + 'px' | |
}) | |
</script> |
@p01 I prefer not to change "this" in callbacks. Сonsidered this option, but decided not to touch "this". I'll put yours nearby.
since you've got a ton of space here, you should consider using the node-style (err, data) signature for your callback.
Save 18 bytes: