views:

320

answers:

2

Hi,

I have used Javascript onlaod like this:

function check()
{
   var pic =  new Image();
   pic.src= "images/first.jpg";
   pic.onload =  function()
   {
     alert("Uploaded");
   }
}

This is html code where the function is called.

<input type="button" onclick="check()" value="Check" />

It works for both safari and firefox. But with IE, first time it works but when I click on check Button next time it does not work. It also works when cache is cleared.

Can anyone help me what problem might occur here.

Thanks in advance

+7  A: 

This should not be a problem in IE8.

IE6 (not sure about 7) is notoriously eager to use cached files, and when taking from the cache the load is not correctly calculated (I recall there being an interesting bug report on this, look for it on MS's site).

It can be solved by adding a [useless] parameter that forces a reload of the cached file:

pic.src= "images/first.jpg?nocache="+Math.random()

SamGoody
A random parameter will solve the caching issue. Your case looks like a caching issue only.
Faiz
Thank you so much samgoody and Faiz !!! Its just a cache problem. It's working now. Thanks again :)
A: 

perhaps the onload() is too early?

jquery uses a function

$(document).ready(function(){}

that is executed when the page has finished loading. Perhaps you need some similar function.

ppuschmann
OP is using the correct event handler, it is just IE aggressively caching and then not reporting a load event the 2nd-Nth times
scunliffe