Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Please, could someone tell me why the alert shows undefined in the following page
XML
<html>
<head>
<title>test</title>
<script language="JavaScript">
var theLargeImg;
function loadImage(url)
{
theLargeImg = new Image();
theLargeImg.onload=imageLoaded();
theLargeImg.src = url;
}
function imageLoaded()
{
alert("img height="+theLargeImg.Height+";img width="+theLargeImg.Width+";complete = "+theLargeImg.complete+&"; src= "+theLargeImg.src);
}
</script>

</head>
<body>
<input type="button" onclick="javascript:loadImage('http://www.google.co.uk/intl/en_ALL/images/logos/images_logo_lg.gif')" value="Load Image">
</body>
</html>

Thanks,
Richard.
Posted
Updated 28-Dec-10 18:45pm
v2

1 solution

1. u should put event onload after the "src".
C#
function loadImage(url)
      {
       theLargeImg = new Image();
       theLargeImg.src = url;
       theLargeImg.onload=imageLoaded();
      }

2. "Height" and "Width" ,both first letter should be low-case.
 
Share this answer
 
Comments
Richard Bushill 28-Dec-10 20:56pm    
Hmm, still does not seem to work and this article

http://www.thefutureoftheweb.com/blog/image-onload-isnt-being-called

seems to disagree, thanks for the answer though, any further thoughts ?
stone__ 29-Dec-10 1:48am    
i'm sorry,i made a mistake.
function loadImage(url)
{
theLargeImg = new Image();
theLargeImg.onload=imageLoaded(); --> theLargeImg.onload=imageLoaded;
theLargeImg.src = url;
}
i try this code in ie7 and firefox.the event are triggered.
but in ie7 "theLargeImg.complete" are false,in ff are true.
Richard Bushill 29-Dec-10 10:04am    
Yes, exactly what I was finding except I tested a load of different browsers all of which handled complete differently, most of which fired the event before the image was fully loaded. The solution I am using is to test the image width for a value of 0 before concluding that the image has indeed finished downloading as follows :
function imageLoaded()
{
if(!theLargeImg.complete || theLargeImg.width==0)
{
setTimeout ( "imageLoaded()", 500);
//alert("delay because image complete="+theLargeImg.complete);
return null;
}

alert("img height="+theLargeImg.Height+";img width="+theLargeImg.Width+";complete = "+theLargeImg.complete+&"; src= "+theLargeImg.src);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900