473,569 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closure code to assign image index to onload handler

I'm writing some code that loads a number of images, using the
standard mechanism of assigning a src url to a newly constructed Image
object, thus invoking the image-load operation. On a successful load,
an image.onload handler I've previously assigned will be called. Or
should be at any rate.

The problem is that I need to know inside the onload handler which
particular image in the array invoked the handler. I'd be quite happy
to know the array index of the successfully loaded image, but since
onload handler assignments can't take a parameter, I can't assign the
index when I set up the onload call. So I'm a bit stumped.

The answer seems to be to use a closure to supply the index of the
invoking image when I set up the handler, but I'm still struggling a
bit w/ closures and can't seem to be able to get the syntax right. Can
anyone supply a snippet of code to show how to do this?

Any help would be much appreciated!
Thanks,
Howard
Oct 31 '08 #1
7 3145
On Nov 1, 12:08*am, howardk <howar...@gmail .comwrote:
I'm writing some code that loads a number of images, using the
standard mechanism of assigning a src url to a newly constructed Image
object, thus invoking the image-load operation. On a successful load,
an image.onload handler I've previously assigned will be called. Or
should be at any rate.

The problem is that I need to know inside the onload handler which
particular image in the array invoked the handler. I'd be quite happy
to know the array index of the successfully loaded image, but since
onload handler assignments can't take a parameter, I can't assign the
index when I set up the onload call. So I'm a bit stumped.

The answer seems to be to use a closure to supply the index of the
invoking image when I set up the handler, but I'm still struggling a
bit w/ closures and can't seem to be able to get the syntax right. Can
anyone supply a snippet of code to show how to do this?
var imgObject = somehowCreateIm ageObject();
imgObject.src = uriPath;
imgObject.onloa d = function () {
onloadHandler(i mgObject);
}
/*
but remember, you're not passing the value of the variable here,
you're passing the variable itself. So if used in a loop all
the handlers will get the last value of imgObject. But you can
create another closure to pass a reference to that specific
instance of the variable like so:
*/

(function (x) {
imgObject.onloa d = function () {
onloadHandler(x );
}
})(imgObject); // <-- here you pass a ref to imgObject as x
Oct 31 '08 #2
On Oct 31, 3:52*pm, slebetman <slebet...@gmai l.comwrote:
On Nov 1, 12:08*am, howardk <howar...@gmail .comwrote:
I'm writing some code that loads a number of images, using the
standard mechanism of assigning a src url to a newly constructed Image
object, thus invoking the image-load operation. On a successful load,
an image.onload handler I've previously assigned will be called. Or
should be at any rate.
Should in some browsers.
>
The problem is that I need to know inside the onload handler which
particular image in the array invoked the handler. I'd be quite happy
to know the array index of the successfully loaded image, but since
onload handler assignments can't take a parameter, I can't assign the
index when I set up the onload call. So I'm a bit stumped.
The answer seems to be to use a closure to supply the index of the
invoking image when I set up the handler, but I'm still struggling a
bit w/ closures and can't seem to be able to get the syntax right. Can
anyone supply a snippet of code to show how to do this?

var imgObject = somehowCreateIm ageObject();
Now there is a black box.
imgObject.src = uriPath;
imgObject.onloa d = function () {
* onloadHandler(i mgObject);

}

/*
* but remember, you're not passing the value of the variable here,
* you're passing the variable itself. So if used in a loop all
You mean a reference to the image?
* the handlers will get the last value of imgObject. But you can
* create another closure to pass a reference to that specific
* instance of the variable like so:
*/

(function (x) {
* imgObject.onloa d = function () {
* * onloadHandler(x );
* }

})(imgObject); // <-- here you pass a ref to imgObject as x

This will leak memory in IE (and is needlessly complex.)

How about:

imgObject.onloa d = onloadHandler;

Then refer to the image object as "this".
Oct 31 '08 #3
On Oct 31, 12:36*pm, David Mark <dmark.cins...@ gmail.comwrote:
On Oct 31, 3:52*pm, slebetman <slebet...@gmai l.comwrote:
On Nov 1, 12:08*am, howardk <howar...@gmail .comwrote:
I'm writing some code that loads a number of images, using the
standard mechanism of assigning a src url to a newly constructed Image
object, thus invoking the image-load operation. On a successful load,
an image.onload handler I've previously assigned will be called. Or
should be at any rate.

Should in some browsers.
The problem is that I need to know inside the onload handler which
particular image in the array invoked the handler. I'd be quite happy
to know the array index of the successfully loaded image, but since
onload handler assignments can't take a parameter, I can't assign the
index when I set up the onload call. So I'm a bit stumped.
The answer seems to be to use a closure to supply the index of the
invoking image when I set up the handler, but I'm still struggling a
bit w/ closures and can't seem to be able to get the syntax right. Can
anyone supply a snippet of code to show how to do this?
var imgObject = somehowCreateIm ageObject();

Now there is a black box.
imgObject.src = uriPath;
imgObject.onloa d = function () {
* onloadHandler(i mgObject);
}
/*
* but remember, you're not passing the value of the variable here,
* you're passing the variable itself. So if used in a loop all

You mean a reference to the image?
* the handlers will get the last value of imgObject. But you can
* create another closure to pass a reference to that specific
* instance of the variable like so:
*/
(function (x) {
* imgObject.onloa d = function () {
* * onloadHandler(x );
* }
})(imgObject); // <-- here you pass a ref to imgObject as x

This will leak memory in IE (and is needlessly complex.)

How about:

imgObject.onloa d = onloadHandler;

Then refer to the image object as "this".
Here's what I finally ended up doing, and it seems to work ok:

function imageLoaded( n ) {
return function( ) {
if ( window.console ) {
window.console. log( 'You\' just loaded image# ' + n );
// do stuff with images[ n ] image object
}
}
};

function loadAllImages() {
var image = new Image();
image.onload = imageLoaded(i);
...
}
Nov 1 '08 #4
slebetman wrote:
var imgObject = somehowCreateIm ageObject();
imgObject.src = uriPath;
imgObject.onloa d = function () {
onloadHandler(i mgObject);
}

/*
but remember, you're not passing the value of the variable here,
you're passing the variable itself.
Nonsense.
So if used in a loop all
the handlers will get the last value of imgObject.
The reason for that would be late resolution of the variable identifier
instead, supported by the closure that the function expression creates.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 2 '08 #5
On Nov 1, 6:28*pm, howardk <howar...@gmail .comwrote:
On Oct 31, 12:36*pm, David Mark <dmark.cins...@ gmail.comwrote:
On Oct 31, 3:52*pm, slebetman <slebet...@gmai l.comwrote:
On Nov 1, 12:08*am, howardk <howar...@gmail .comwrote:
I'm writing some code that loads a number of images, using the
standard mechanism of assigning a src url to a newly constructed Image
object, thus invoking the image-load operation. On a successful load,
an image.onload handler I've previously assigned will be called. Or
should be at any rate.
Should in some browsers.
The problem is that I need to know inside the onload handler which
particular image in the array invoked the handler. I'd be quite happy
to know the array index of the successfully loaded image, but since
onload handler assignments can't take a parameter, I can't assign the
index when I set up the onload call. So I'm a bit stumped.
The answer seems to be to use a closure to supply the index of the
invoking image when I set up the handler, but I'm still struggling a
bit w/ closures and can't seem to be able to get the syntax right. Can
anyone supply a snippet of code to show how to do this?
var imgObject = somehowCreateIm ageObject();
Now there is a black box.
imgObject.src = uriPath;
imgObject.onloa d = function () {
* onloadHandler(i mgObject);
}
/*
* but remember, you're not passing the value of the variable here,
* you're passing the variable itself. So if used in a loop all
You mean a reference to the image?
* the handlers will get the last value of imgObject. But you can
* create another closure to pass a reference to that specific
* instance of the variable like so:
*/
(function (x) {
* imgObject.onloa d = function () {
* * onloadHandler(x );
* }
})(imgObject); // <-- here you pass a ref to imgObject as x
This will leak memory in IE (and is needlessly complex.)
How about:
imgObject.onloa d = onloadHandler;
Then refer to the image object as "this".

Here's what I finally ended up doing, and it seems to work ok:

function imageLoaded( n ) {
* * return function( ) {
* * * * *if ( window.console ) {
* * * * * * *window.console .log( 'You\' just loaded image# ' + n );
* * * * * * *// do stuff with images[ n ] image object
* * * * *}
* * }

};

function loadAllImages() {
* * var image = new Image();
* * image.onload = imageLoaded(i);
* * ...

}

Why not:

function imageLoaded() {
window.alert(th is.src);
}

var image = new Image();
image.onload = imageLoaded;
....

And make sure the Image constructor exists before calling it.
Nov 3 '08 #6
David Mark wrote:
On Nov 1, 6:28 pm, howardk <howar...@gmail .comwrote:
>Here's what I finally ended up doing, and it seems to work ok:

function imageLoaded( n ) {
return function( ) {
if ( window.console ) {
window.console. log( 'You\' just loaded image# ' + n );
// do stuff with images[ n ] image object
}
}

};

function loadAllImages() {
var image = new Image();
image.onload = imageLoaded(i);
...

}

Why not:

function imageLoaded() {
window.alert(th is.src);
}

var image = new Image();
image.onload = imageLoaded;
...
Two good reasons:

1. There we have a global method to no obvious advantage.

2. The OP wanted to know the *index* of the image object or data
related thereto in *another* data structure, not its URI.
I would presume something should happen to that image or for it
when it was loaded.

An application that comes to mind, since I have been dealing
with it before, is loading the high-resolution version of an
image when the low-res version has finished loading. To do
that, it doesn't help to know the URI of the low-res image,
you need a reference to the target (HTML)Image(Ele ment) object;
the index of the image object helps to retrieve that.
And make sure the Image constructor exists before calling it.
Full ACK.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 3 '08 #7
On Nov 3, 6:24*am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
David Mark wrote:
On Nov 1, 6:28 pm, howardk <howar...@gmail .comwrote:
Here's what I finally ended up doing, and it seems to work ok:
function imageLoaded( n ) {
* * return function( ) {
* * * * *if ( window.console ) {
* * * * * * *window.console .log( 'You\' just loaded image# ' + n );
* * * * * * *// do stuff with images[ n ] image object
* * * * *}
* * }
};
function loadAllImages() {
* * var image = new Image();
* * image.onload = imageLoaded(i);
* * ...
}
Why not:
function imageLoaded() {
* *window.alert(t his.src);
}
var image = new Image();
image.onload = imageLoaded;
...

Two good reasons:

1. There we have a global method to no obvious advantage.
Well, it doesn't have to be global. As written, yes.
>
2. The OP wanted to know the *index* of the image object or data
* *related thereto in *another* data structure, not its URI.
* *I would presume something should happen to that image or for it
* *when it was loaded.
If he has data stored about the image, then yes that would be a good
case for using the other solution.

[snip]
Nov 3 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3893
by: TheKeith | last post by:
I'm doing an image cycler but can't figure out why it keeps getting hung up on the third pic in the array? Here is what I have: ---------------------------------------------------------------------------- --------------------------------- <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html;...
1
2993
by: Roger Shrubber | last post by:
Good day In an image onload handler, how do I access the image that just loaded? If the image is attached to the document, I can access it using the field window.event.srcElement. But I really don't want to attach the image to the document until it has loaded (I plan to use it to replace an existing visible low-res version of the same...
2
1910
by: Charles Harrison Caudill | last post by:
I'm trying to write a function to determine whether or not an image exists. Most people recommend setting the onload event handler. That works like a charm except that side-effective actions seem to be impossible. I can, as nearly as I can tell, run alert and opera.postError (I test on IE, firefox, and opera by the way). I've tried using...
2
6439
by: Martin Honnen | last post by:
I was playing around with canvas support in recent Safari, Mozilla and Opera (only version 9 preview) but run into issues with Safari related to the very old DOM Level 0 Image object for preloading images. When doing e.g. var img = new Image(); img.onload = function (evt) { alert(this); }; img.src = 'whatever.gif';
1
2031
by: laurakr | last post by:
I am trying to use a clear to get my bottom nav bar below the quote box on the right, but it isn't working. I would like the bottom edge of the quote box to "stick" to the footer nav bar but copy to be able to flow to the left of the quote box. For the bonus round I would like the right edge of the box to align with the right edge of the...
53
4638
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating...
11
1868
by: Adam Sandler | last post by:
Hello, Having an issue with JavaScript in my ASP.Net page. I use some COTS, which for all intents and purposes, simply makes a jpeg file and physically places it in a directory on the web server. The page load codebehind, makes a connection to the jpeg creation service and gets the path of the newly created image.
11
1763
by: webgour | last post by:
Hello, I am wondering why the following works, on IE6, but with an error : "Not implemented". function TEST(){} TEST.prototype.Initialize = function() { var mImage = new Image(); var mDate = new Date();
7
2247
by: AAaron123 | last post by:
I read the help on which says: The ClientScriptManager class is used to manage client-side scripts and add them to Web applications... But could use a little help. Can someone tell me what the code below is used for? If you could just put a few comments into the code that would help.
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.