Eli, Boris,
The solution proposed by Eli has the advantages of being simple and
working reliably. If anyone else end up needing something like this,
here is that the resulting code looks like [1].
Thank you for your support and responsiveness!!!
[1]
////////////////////////////////////////////
// Image Utils //
////////////////////////////////////////////
var ImageUtils = function()
{
var workingSet = {};
var that = {};
that.askImageSize = function( url, callbackFunc )
{
// somebody has already requested this image
if( workingSet[ url ] != null )
{
workingSet[ url ].push( callbackFunc );
return;
}
// DBH $debug( "Asking size information for: " + url );
workingSet[ url ] = [ callbackFunc ];
var aImage = new Image();
aImage.onload = function( )
{
that.onImageLoaded(
this.src, this.width, this.height );
}
aImage.src = url;
}
that.onImageLoaded = function ( url, width, height )
{
// DBH $debug( "ImageUtils]" + url + " = " + width + " x " +
height );
var requestors = workingSet[ url ];
if( requestors == null )
$debug( "BUG: There is now requestors for: " + url );
else
{
// callback requestors
for( var i = 0; i < requestors.length; i++ )
requestors[ i ].call( this, url, width, height
);
// update working set
delete workingSet[ url ];
}
}
that.runUnitTests = function()
{
function getImageSize( aURL )
{
that.askImageSize( aURL,
function( url,
width, height )
{
$debug(
"The result is:" + url + " -- w:"+ width + " -- h: "
+ height );
}
);
}
getImageSize( "http://www.techcrunch.com/wp-content/
photobucketlogo210.gif" );
getImageSize( "http://farm1.static.flickr.com/
126/384986014_74f575caa0_o.jpg" );
getImageSize( "http://farm1.static.flickr.com/
131/344620838_def1dc77f4_o.jpg" );
getImageSize( "http://farm1.static.flickr.com/
144/344620839_9cc5ff4b6e_o.jpg" );
}
return that;
}();
_______________________________________________
dev-tech-layout mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-layout