473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

image.onload in Firefox

jon
Hello,
I've had long standing code that runs in IE, that I'm testing with
firefox unsuccessfully now. The problem seems to be that images that I
dynamically create don't fire their onload event in firefox. The
onload method I assign is never being called. Any ideas why firefox
isn't calling this, or what I can do for a workaround?

Below is approxiatemate code of what I'm doing...
function addImage(obj,isTrue){
alert('add');
alert(obj.id);
// do stuff
}
function main(){
var imageObj = new Array();
var image_src = '/images/someKnownImage.jpg';
var somethingIsTrue = true;

imageObj[imageObj.length] = new Image();
imageObj[imageObj.length-1].id = id;
if(somethingIsTrue){
imageObj[imageObj.length-1].onload = function() {
addImage(this,'true'); }
} else {
imageObj[imageObj.length-1].onload = function() {
addImage(this,'false'); }
}
imageObj[imageObj.length-1].src = image_src;

}

Jul 19 '06 #1
3 26729

jon wrote:
Hello,
I've had long standing code that runs in IE, that I'm testing with
firefox unsuccessfully now. The problem seems to be that images that I
dynamically create don't fire their onload event in firefox. The
onload method I assign is never being called. Any ideas why firefox
isn't calling this
According to the HTML 4 specification, img elements don't have an
onload attribute.

<URL:http://www.w3.org/TR/html4/struct/objects.html#edef-IMG>
However IE, Opera and Firefox at least seem to have implemented it.

or what I can do for a workaround?

Below is approxiatemate code of what I'm doing...
Your code runs fine for me - check Firefox's JavaScript console.
>

function addImage(obj,isTrue){
alert('add');
alert(obj.id);
You don't assign an ID, so the above alerts 'undefined' in Firefox and
throws an error in IE.

// do stuff
}
function main(){
var imageObj = new Array();
var image_src = '/images/someKnownImage.jpg';
var somethingIsTrue = true;

imageObj[imageObj.length] = new Image();
Why continually get imageObj.length? It's probably best to use a var
to store the object temporarily, then assign it to the array at the
end.

imageObj[imageObj.length-1].id = id;
You don't assign a value to id, so nothing sensible is assigned to the
value of the image object.

Here's my cleaned-up version, maybe the error will become apparent:

function addImage(obj, isTrue){
alert('add');
alert('id: ' + obj.id
+ '\nisTrue: ' + isTrue );
}

function main(){
var imageObj = new Array();
var image_src = 'a.gif';
var somethingIsTrue = true;
var id = 'blah';

var img = new Image();
img.id = id;
img.onload = (function(bool){
return function(){addImage(this, bool);}
})(somethingIsTrue);
img.src = image_src;
imageObj[imgObj.length] = img;
}

main();
--
Rob

Jul 19 '06 #2
function main(){
var imageObj = new Array();
var image_src = 'a.gif';
var somethingIsTrue = true;
var id = 'blah';

var img = new Image();
img.id = id;
img.onload = (function(bool){
return function(){addImage(this, bool);}
})(somethingIsTrue);
img.src = image_src;
imageObj[imgObj.length] = img;
}
Be careful using inline functions. If at any time you need to delete
the image, the image will delete fine, but the browser won't free up
the memory. Bascially the inline function has a reference to the parent
function and the parent function has a reference to the inline
function. So when you try to removeChild() or delete() the memory won't
be freed.

You'll better off not using inline functions at all as good practice.
It can lead to all sorts of nasty memory leaks if you are building any
systems that use lots of dom manipulation.

But I digress :)

Jul 19 '06 #3
Pete wrote:
> function main(){
var imageObj = new Array();
var image_src = 'a.gif';
var somethingIsTrue = true;
var id = 'blah';

var img = new Image();
img.id = id;
img.onload = (function(bool){
return function(){addImage(this, bool);}
})(somethingIsTrue);
img.src = image_src;
imageObj[imgObj.length] = img;
}

Be careful using inline functions. If at any time you need
to delete the image,
Javascript has no mechanism for deleting the image, it has automatic
garbage collection.
the image will delete fine, but the browser won't
free up the memory.
A strange definition of 'delete'.
Bascially the inline function has a reference to the
parent function and the parent function has a reference
to the inline function.
The parent function does not have a reference to the inner function, it
being anonymous. IE's circular reference issue is provoked here by the
Variable object of the outer function still having a reference to the
Image object itself (as its - img - variable). The circle is outer
Variable.img -Image, Image,onload -inner function, inner
function.[[Scope]] -outer Variable. That circle can be broken by
ending the outer function with - imageObj = img = null; -, but IE may
take the assignment of an - id - to the image as an excuse to add a
property to the global object with the name of the - id - and referring
to the Image, forming a second circular reference through the global
object.
So when you try to removeChild() or delete() the memory
won't be freed.
Which environments provide a - delete - function/method? And how would
it be used with javascript where - delete - (being an operator) cannot
be an Identifier name?
You'll better off not using inline functions at all as
good practice.
That would be paranoid and self-destructive. Once the memory leak issue
in IE is understood, along with the mechanism of closures in javascript,
action can be taken to avoid and mitigate the issue.
It can lead to all sorts of nasty memory leaks if you are
building any systems that use lots of dom manipulation.
In this code there is no evidence of any DOM manipulation happening at
all. However, inner function's are nowhere near the only means of
provoking memory leaks in IE. Any chain of references that is circular
and includes a DOM/ActiveX object will hinder the garbage collection of
the objects involved.

For example, an action as apparently simple as defining an element in
the HTML with an ID attribute and an intrinsic event handing attribute
may be enough. As IE will turn the intrinsic event attribute code into a
function that is referred to by a property of the element, IE will use
the ID attribute to create a property of the global object of the global
object which refers to the element, and the global object is referred to
by the internal [[Scope]] property of all functions the circle is:
Element.onevent -event handler function, event handler
function.[[Scope]] -global object, global object[id] -Element.

It is short sighted to think that the issue may be avoided by a blanket
ban on the use of inner functions (particularly as event handlers).

Richard.
Jul 19 '06 #4

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

Similar topics

2
by: erikv | last post by:
I'm having a hard time figuring out how to get the width of an image I replace using the following code: document.getElementById("sizeImg").src="http://www.example.com/image.jpg"; After this...
1
by: Adam Ratcliffe | last post by:
I'm trying to come up with a solution for detecting when an image, loaded by a script, has completely loaded. The Image.onload event is fired after the image has loaded in Firefox but before...
1
by: bjarthur | last post by:
i have (see below) what i think is a fairly simple algorithm, but yet it doesn't work. given a directory with consecutively numbered jpeg files (1.jpg, 2.jpg, 3.jpg...), it is designed to count...
4
by: zborisau | last post by:
Hey good people, I've been given a problem to solve recently - and stuck with the solution for a good 4 days already. i have a link which leads to popup window. the purpose of that popup...
2
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...
7
by: cjl | last post by:
OK: I am really scratching my head over a preload / image swapping problem, so I started conducting experiments: http://www.saintrays.net/experiment1.html...
11
by: DDL | last post by:
I am trying to create a simple web gallery to swap my images at http://dev.lenosphotography.com Go to the family section (should open a image bar at bottom - you may have to scroll down - I have...
4
by: Weston | last post by:
I've been playing around with some means of testing image loading, and noticing that the "complete" property seems to default to true. Trying the following in the squarefree javascript shell: I...
2
by: Atul | last post by:
I am unable to find image height and width in mozilla firefox. My code is working in IE but not in Mozilla. How can i find image width and height in mozilla? function check(sel) { if(sel != "")...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.